我正在尋找一種方法來改善一種更好的方式來編寫一段代碼,看看是否有客戶端來自「第二列表」中的「第一列表」並將數據複製過來到名爲「找到」的工作表。如何使用設置範圍
它是這樣的:
Dim row As Long, row2 As Long, found as Long
Dim ID As String, prtGtId as String, GtId2 as String
Application.ScreenUpdating = False
prtGtId = "B"
GtId2 = "D"
row = 2
row2 = 2
found = 0
Do While row <= Cells(Rows.Count, prtGtId).End(xlUp).row
ID = Cells(row, prtGtId)
Sheets("Second List").Select
Do While row2 <= Cells(Rows.Count, GtId2).End(xlUp).row
If (ID = Cells(row2, GtId2)) Then
Rows(row2).Select
Selection.Copy
Sheets("Found").Select
Rows(2).Select
Selection.Insert Shift:=xlDown
Sheets("First List").Select
Rows(row).Select
Selection.Copy
Sheets("Found").Select
Rows(2).Select
Selection.Insert Shift:=xlDown
Sheets("Second List").Select
found = found + 1
End If
row2 = row2 + 1
Loop
Sheets("First List").Select
row = row + 1
row2 = 2
Loop
Sheets("Blank").Select
Cells(2, 3) = found
Application.ScreenUpdating = True
這是我的實際代碼的簡化版本(這是更大的,並含有大量的早期退出的循環,以及按字母順序組織),但右現在我主要擔心使用「.select」函數是消耗大部分處理時間的東西。
我知道我已經看到有使用
Set rng = Range(Cells(2, prtGtId),Cells(Cells(Rows.Count, GtId2).End(xlUp).row, prtGtId))
For Each Cell in rng
Code
Next Cell
或東西沿着這些線路的另一種方式,但我似乎無法找到一個詳細的教程,還遠不止「這將大大加快這種方式」 。
考慮到我在複製到「Found」時需要保留行中的格式,有沒有辦法改變這個以使其更快?
請勿將[宏]用於MS Office或VBA。 [macros tag wiki](http://stackoverflow.com/tags/macros/info) –