[英]Excel VBA Nested Loop Issue
真的希望有人能幫助我。
我是VBA的新手,幾周后才開始學習。 我正在嘗試創建一個嵌套循環以合並Excel中多個選項卡中的數據。
這是基於工作表名稱“>”和第3列,其中包含True或False值。 然后將數據粘貼到稱為導入的工作表中。
這兩個循環似乎可以獨立工作,但我無法讓它們一起工作。
提前致謝。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.