【问题标题】:Copy rows from multiple sheets into one sheet将多张工作表中的行复制到一张工作表中
【发布时间】:2016-03-23 02:30:58
【问题描述】:
Public Sub SubName()
Dim ws As Worksheet
Dim iCounter As Long
Dim wso As Worksheet
Dim rw As Long
Dim lastrow As Long

Set wso = Sheets("Master")

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*" & "danger" & "*" Then
    ws.Select
    lastrow = ws.Cells(Rows.Count, 4).End(xlUp).Row
            For iCounter = 2 To lastrow
                If ws.Cells(iCounter, 8) < 0.15 And ws.Cells(iCounter, 8) > -0.1 Then

                    ws.Cells(iCounter, 8).EntireRow.Copy

                    rw = wso.Cells(wso.Rows.Count, "A").End(xlUp).Row + 1
                    wso.Cells(rw, 1).PasteSpecial Paste:=xlPasteAll

                End If
            Next iCounter

    End If
Next ws
End Sub

这就是代码的作用:

  1. 查看所有工作表,找到带有“危险”文本的工作表
  2. 使用名为“danger*”的工作表,遍历 H 列并在满足条件时复制整行
  3. 将整行粘贴到主表上

我相信代码可以正常工作,直到我需要将其粘贴到主工作表上。我遇到的问题是它只是粘贴在主工作表上的同一行上,而不是行+1。

最终结果是主工作表上只显示一行,它是要粘贴的迭代中的最后一行。

感谢任何帮助!

【问题讨论】:

  • instead of going row+1. 所以你知道你需要做什么 XD 编辑:哦,rw 已经是 +1
  • 正在复制的数据中,A列有数据吗?
  • @ScottCraner,我是个傻瓜...... 修复了它。非常感谢!
  • 你为什么不试试rw = rw +1
  • @ScottCraner,我们都应该有rubber duckys

标签: vba excel


【解决方案1】:

尝试将行从一个工作表复制到另一个工作表(每一行位于不同的行):

Dim i, lastRow as Integer

Dim wso, ws, copyFrom as Worksheet

Set wso = Sheets("Master")

For Each ws In ActiveWorkbook.Worksheets

If ws.Name Like "*" & "danger" & "*" Then

    copyFrom =  ws.Select

End if


lastRow = Sheets(copyFrom).Range("A" & Rows.Count).End(xlUp).Row
For i=1 to lastRow

If <--your criteria--> then

    ThisWorkbook.Sheets(copyFrom).Rows(i).EntireRow.Copy Destination:=ThisWorkbook.Sheets(wso).Rows(i)

End if

Next i
Next ws

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多