简体   繁体   English

使用高级过滤器加速从 VBA 中的另一张表复制

[英]Speeding up copying from another sheet in VBA using advanced filter

I'm very new to VBA and this community so I hope I'm not asking any stupid question and I apologize beforehand if the phrasing of my question isn't up to the standards.我对 VBA 和这个社区非常陌生,所以我希望我没有问任何愚蠢的问题,如果我的问题的措辞不符合标准,我提前道歉。

I've been working on a code with the intention of going through a column and copying values from a column in a second sheet to an empty column in my main sheet based on a criteria, but I'm having a bit of a problem with the speed of this code since it takes a long time for me to see the results (sometimes it even crashes depending on the size of the data) Here's the concerning part of the code:我一直在编写代码,目的是根据标准遍历列并将第二张表中的列中的值复制到主表中的空列中,但是我遇到了一些问题这段代码的速度,因为我需要很长时间才能看到结果(有时它甚至会崩溃,具体取决于数据的大小)这是代码的相关部分:

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

As you can see, i already tried the GoTo NextIteration method which significantly reduced the time needed to execute the code, but I was wondering if there is a better/faster method to do this especially after learning about the speed of the AdvancedFilter function in VBA.如您所见,我已经尝试过 GoTo NextIteration 方法,它显着减少了执行代码所需的时间,但我想知道是否有更好/更快的方法来执行此操作,尤其是在了解了 VBA 中 AdvancedFilter function 的速度之后.

Also, please feel free to give me any instruction or advice even if it's not related to the main issue.另外,请随时给我任何指示或建议,即使它与主要问题无关。

Have a lovely evening.有一个美好的夜晚。

As stated in the comments, VLOOKUP may be your friend here.如评论中所述,VLOOKUP 可能是您的朋友。 If, for some reason, you must programmatically append the "added data" column, here is a code example.如果由于某种原因,您必须以编程方式 append “添加数据”列,这里是一个代码示例。

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$2:$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM