使用高级过滤器加快从 VBA 中另一个 sheet 的复制
Speeding up copying from another sheet in VBA using advanced filter
我对 VBA 和这个社区还很陌生,所以我希望我没有问任何愚蠢的问题,如果我的问题的措辞不符合标准,我会事先道歉。
我一直在编写一个代码,目的是通过一个列并在一秒钟内将列中的值复制 sheet 到我的主要 sheet 中的一个空列基于标准,但我对这段代码的速度有点问题,因为我需要很长时间才能看到结果(有时它甚至会崩溃,具体取决于数据的大小)
这是代码的相关部分:
Dim x1 As Integer
Worksheets("A").Activate
x1 = ActiveSheet.UsedRange.Columns.Count
'Add a column in the end to put the add data
Worksheets("A").Cells(1, x1 + 1) = "added data"
Dim i As Integer
Dim j As Integer
Dim N1 As Integer
Dim N2 As Integer
N1 = Worksheets("A").Cells(Rows.Count, 1).End(xlUp).Row
N2 = Worksheets("B").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To N1
For j = 2 To N2
If Worksheets("B").Cells(j, 1).Value = Worksheets("A").Cells(i, 3).Value Then
Worksheets("A").Cells(i, x1 + 1).Value = Worksheets("B").Cells(j, 3).Value
GoTo NextIteration
Else
End If
NextIteration:
Next j
Next i
如您所见,我已经尝试了 GoTo NextIteration 方法,该方法显着减少了执行代码所需的时间,但我想知道是否有 better/faster 方法可以做到这一点,尤其是在了解了VBA.
中 AdvancedFilter 函数的速度
此外,即使与主要问题无关,也请随时给我任何指导或建议。
祝你有个愉快的夜晚。
如评论中所述,VLOOKUP 可能是您的朋友。如果出于某种原因,您必须以编程方式附加“添加的数据”列,这里是一个代码示例。
Public Sub Test()
Dim wsA As Excel.Worksheet
Dim usedRngA As Excel.Range
Dim formulasRange As Excel.Range
Dim targetColIndex As Long
Dim targetLastRowIndex As Long
Dim sourceLastRowIndex As Long
Dim lookupFormula As String
Set wsA = ThisWorkbook.Worksheets("A")
Set usedRngA = wsA.UsedRange
'Column header.
targetColIndex = usedRngA.Columns.Count + 1
wsA.Cells(1, targetColIndex).Value = "added data"
'Formulas.
targetLastRowIndex = usedRngA.Rows.Count
sourceLastRowIndex = ThisWorkbook.Worksheets("B").UsedRange.Rows.Count
'Check if we have anything to work with.
If targetLastRowIndex > 1 And sourceLastRowIndex > 1 Then
lookupFormula = "=IFERROR(VLOOKUP(C2,B!$A:$C$" & sourceLastRowIndex & ",3,FALSE), ""Not Found"")"
Set formulasRange = wsA.Range(wsA.Cells(2, targetColIndex), wsA.Cells(targetLastRowIndex, targetColIndex))
formulasRange.Formula = lookupFormula
End If
'If desired, eliminate formulas.
formulasRange.Value = formulasRange.Value
End Sub
我对 VBA 和这个社区还很陌生,所以我希望我没有问任何愚蠢的问题,如果我的问题的措辞不符合标准,我会事先道歉。
我一直在编写一个代码,目的是通过一个列并在一秒钟内将列中的值复制 sheet 到我的主要 sheet 中的一个空列基于标准,但我对这段代码的速度有点问题,因为我需要很长时间才能看到结果(有时它甚至会崩溃,具体取决于数据的大小) 这是代码的相关部分:
Dim x1 As Integer
Worksheets("A").Activate
x1 = ActiveSheet.UsedRange.Columns.Count
'Add a column in the end to put the add data
Worksheets("A").Cells(1, x1 + 1) = "added data"
Dim i As Integer
Dim j As Integer
Dim N1 As Integer
Dim N2 As Integer
N1 = Worksheets("A").Cells(Rows.Count, 1).End(xlUp).Row
N2 = Worksheets("B").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To N1
For j = 2 To N2
If Worksheets("B").Cells(j, 1).Value = Worksheets("A").Cells(i, 3).Value Then
Worksheets("A").Cells(i, x1 + 1).Value = Worksheets("B").Cells(j, 3).Value
GoTo NextIteration
Else
End If
NextIteration:
Next j
Next i
如您所见,我已经尝试了 GoTo NextIteration 方法,该方法显着减少了执行代码所需的时间,但我想知道是否有 better/faster 方法可以做到这一点,尤其是在了解了VBA.
中 AdvancedFilter 函数的速度此外,即使与主要问题无关,也请随时给我任何指导或建议。
祝你有个愉快的夜晚。
如评论中所述,VLOOKUP 可能是您的朋友。如果出于某种原因,您必须以编程方式附加“添加的数据”列,这里是一个代码示例。
Public Sub Test()
Dim wsA As Excel.Worksheet
Dim usedRngA As Excel.Range
Dim formulasRange As Excel.Range
Dim targetColIndex As Long
Dim targetLastRowIndex As Long
Dim sourceLastRowIndex As Long
Dim lookupFormula As String
Set wsA = ThisWorkbook.Worksheets("A")
Set usedRngA = wsA.UsedRange
'Column header.
targetColIndex = usedRngA.Columns.Count + 1
wsA.Cells(1, targetColIndex).Value = "added data"
'Formulas.
targetLastRowIndex = usedRngA.Rows.Count
sourceLastRowIndex = ThisWorkbook.Worksheets("B").UsedRange.Rows.Count
'Check if we have anything to work with.
If targetLastRowIndex > 1 And sourceLastRowIndex > 1 Then
lookupFormula = "=IFERROR(VLOOKUP(C2,B!$A:$C$" & sourceLastRowIndex & ",3,FALSE), ""Not Found"")"
Set formulasRange = wsA.Range(wsA.Cells(2, targetColIndex), wsA.Cells(targetLastRowIndex, targetColIndex))
formulasRange.Formula = lookupFormula
End If
'If desired, eliminate formulas.
formulasRange.Value = formulasRange.Value
End Sub