Excel VBA嵌套循环问题



真的希望有人能帮助我。

我是VBA的新手,几周前才开始学习。我正试图创建一个嵌套循环来合并Excel中多个选项卡的数据。

这是基于以">"开头的工作表名称和包含True或False值的第3列。然后将数据粘贴到名为导入的工作表中。

这两个循环似乎是独立工作的,但我无法让它们一起工作。

提前谢谢。

Sub TestImport()
Dim WS As Worksheet
Dim Criteria As Boolean
Dim C As Integer
Criteria = True
Application.ScreenUpdating = False
Sheets("Import").UsedRange.Offset(1).Clear
For Each WS In ThisWorkbook.Worksheets
If Left(WS.Name, 1) = ">" Then
    For C = 2 To WS.Range("A1000").End(xlUp).Row
    If WS.Cells(C, 3) = Criteria Then
        WS.Range(Cells(C, 5), Cells(C, 12)).Copy
        Worksheets("Import").Range("A100").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
        End If
        Next C
    End If
Next WS
Application.ScreenUpdating = True
Sheets("Import").Select
End Sub

由于您正在工作簿中的工作表之间循环,因此您应该完全限定正在使用的任何范围。具体而言,以下行:

WS.Range(Cells(C, 5), Cells(C, 12)).Copy

应更改为:

WS.Range(WS.Cells(C, 5), WS.Cells(C, 12)).Copy

最新更新