所以這裏是我的代碼,即時試圖比較一個包含3000行與另一個5000行的工作表,但它工作太慢,任何人都可以幫忙嗎?VBA從兩張紙比較太慢?
Dim G As Long
Dim K As Long
Dim CardBrand As String
Dim STD As String
Dim CardBrand2 As String
Dim ASI
Dim ID As String
Dim X As Workbook
Dim FinalRow As Long
Dim Finalrow2 As Long
Dim I As Long
Dim TC_STD As String
Dim TC_ASI As String
Dim TC_Perc As Double
Dim TC_Base As Double
Dim TC_ID As String
Application.ScreenUpdating = False
FinalRow = Cells(Rows.Count, "I").End(xlUp).Row
For G = 5 To FinalRow
CardBrand = Sheets("sheet1").Cells(G, 9).Value
STD = Sheets("sheet1").Cells(G, 10).Value
ID = Sheets("sheet1").Cells(G, 5).Value
For K = 2 To 51
CardBrand2 = Sheets("sheet2").Cells(K, 3).Value
If CardBrand = CardBrand2 Then
ASI = Sheets("sheet2").Cells(K, 1).Value
Set X = Workbooks.Open("E:\Partner_Commission_Compiler\Repository\Transaction_Charges.xlsx")
Finalrow2 = X.ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For I = 1 To Finalrow2
TC_ASI = X.ActiveSheet.Cells(I, 6).Value
TC_STD = X.ActiveSheet.Cells(I, 11).Value
TC_ID = X.ActiveSheet.Cells(I, 1).Value
If (TC_ASI = ASI) And (TC_STD = STD Or TC_STD = "All") And TC_ID = ID Then
TC_Perc = X.ActiveSheet.Cells(I, 19).Value
TC_Base = X.ActiveSheet.Cells(I, 20).Value
ThisWorkbook1.Sheets("Sheet1").Activate
Sheets("sheet1").Cells(G, 13).Value = TC_Perc
Sheets("sheet1").Cells(G, 14).Value = TC_Base
End If
Next I
End If
Next K
Next G
X.Close (False)
Application.ScreenUpdating = True
如果代碼是縮進的,你的代碼會更容易閱讀和理解。我不能爲此而煩惱,所以這取決於你。 – 2015-03-25 10:51:12
嗨馬克,iv更新它,但縮進不是因爲它應該是有原因或另一個。 – 2015-03-25 11:03:25
這對你來說會是一個很大的改變,但是任何在許多單元上循環的代碼可能需要很長時間,所以在這種情況下我總是使用數組。基本概念是讀取單元的循環的每次迭代實際上是代碼和工作表的軟件層之間的I/O,因此更多的單元=更多的I/O。你是否使用數組方法,將兩張表的內容讀入一個數組(每個數據爲1)= 2個I/O - 然後在代碼中執行任何你想要的操作,也許寫出另一個數組或更新現有的數組,最後,將任何更新或新的陣列寫回您的目標。 3 I/O's – 2015-03-25 11:08:45