我想要使用VLOOKUP或Index Match,将数组中的列转换以加快速度。

huangapple go评论54阅读模式
英文:

I would like to vlookup or Index Match, converting columns in arrays to make it faster

问题

我有Page 1和Page 2。
在Page 2中,我想提供一个状态,指示是否在Page 1中也找到了"Reference Number"。
VLOOKUP和INDEX MATCH非常慢,有人可以帮助我将其转换为数组或类似的东西,以加速吗?

这是我尝试过的,但当行数超过6000行时非常慢。

Sub Calculate()

Set Page1 = Worksheets("Page1")
Set Page2 = Worksheets("Page2")

LastRowPage1 = Page1.Cells(Page1.Rows.Count, "A").End(xlUp).Row
LastRowPage2 = Page2.Cells(Page2.Rows.Count, "A").End(xlUp).Row

For i = 2 To LastRowPage1
    Page2.Range("E" & i) = Application.Match(Page2.Range("D" & i).Value, Page1.Columns(3), 0)

    If IsNumeric(Page2.Range("E" & i).Value) Then
        Page2.Range("E" & i) = "Yes"
    Else
        Page2.Range("E" & i) = "No"
    End If
Next
End Sub

我需要使用VBA,因为这是一个大流程的一小部分。

英文:

I have Page 1 and Page 2.
In Page 2 I would like to give a status that indicates if "Reference Number" is also found in Page1.
Vlook Up and Index Match is very very slow, can someone help me convert in arrays or somthing like that to make it hiper fast?

This is what I tried, but it is ver slow when you have over 6000 rows.

Sub Calculate()

Set Page1 = Worksheets("Page1")
Set Page2 = Worksheets("Page2")

LastRowPage1 = Page1.Cells(Page1.Rows.Count, "A").End(xlUp).Row
LastRowPage2 = Page2.Cells(Page2.Rows.Count, "A").End(xlUp).Row

For i = 2 To LastRowPage1
Page2.Range("E" & i) = Application.Match(Page2.Range("D" & i).Value, Page1.Columns(3), 0)

    If IsNumeric(Page2.Range("E" & i).Value) Then
        Page2.Range("E" & i) = "Yes"
        Else
        Page2.Range("E" & i) = "No"
End If
Next
End Sub

我想要使用VLOOKUP或Index Match,将数组中的列转换以加快速度。

我想要使用VLOOKUP或Index Match,将数组中的列转换以加快速度。

I need it with vba because its a small part of a big process.

答案1

得分: 0

你的代码运行缓慢,因为你逐个计算每个单元格,而且对每个单元格写入两次……这很慢。

相反,你可以使用公式检索结果,然后将结果转换为数值:

Sub Calculate()

Dim Page1 As Worksheet, Page2 As Worksheet

Set Page1 = Worksheets("Page1")
Set Page2 = Worksheets("Page2")

Dim lastRowPage1 As Long, lastRowPage2 As Long
lastRowPage1 = Page1.Cells(Page1.Rows.Count, "A").End(xlUp).Row
lastRowPage2 = Page2.Cells(Page2.Rows.Count, "A").End(xlUp).Row

With Page2.Range("E2:E" & lastRowPage2)
    .Formula = "=IF(ISNA(MATCH(D2,Page1!C2:C" & lastRowPage1 & " ,0)),""no"",""yes"")"
    .Value = .Value
End With

End Sub
英文:

Your code is slow because you calculate each cell individually plus you are writing twice to each cell ... this is very slow.

Instead you can use formula to retrieve the result and then turn the result into values:

Sub Calculate()

Dim Page1 As Worksheet, Page2 As Worksheet

Set Page1 = Worksheets("Page1")
Set Page2 = Worksheets("Page2")

Dim lastRowPage1 As Long, lastRowPage2 As Long
lastRowPage1 = Page1.Cells(Page1.Rows.Count, "A").End(xlUp).Row
lastRowPage2 = Page2.Cells(Page2.Rows.Count, "A").End(xlUp).Row

With Page2.Range("E2:E" & lastRowPage2)
    .Formula = "=IF(ISNA(MATCH(D2,Page1!C2:C" & lastRowPage1 & " ,0)),""no"",""yes"")"
    .Value = .Value
End With

End Sub

答案2

得分: 0

添加另一个选项。您提到将列转换为数组。这是一种可能性,应该非常快速。如果您有机会尝试它以及您得到的其他答案,我对性能差异很感兴趣。

Sub Calculate()

Set page1 = Worksheets("Page1")
Set page2 = Worksheets("Page2")

lastrowpage1 = page1.Cells(page1.Rows.Count, "A").End(xlUp).Row
lastrowpage2 = page2.Cells(page2.Rows.Count, "A").End(xlUp).Row

Dim lookupArr() As Variant, valueArr() As Variant, resultArr() As Variant, rng As Range
Set rng = page1.Range("C2:C" & lastrowpage1)
lookupArr = rng
Set rng = page2.Range("D2:D" & lastrowpage2)
valueArr = rng
ReDim resultArr(1 To UBound(valueArr), 1 To UBound(valueArr, 2))

For i = 1 To UBound(valueArr)
    resultArr(i, 1) = "no"
    For j = 1 To UBound(lookupArr)
        If valueArr(i, 1) = lookupArr(j, 1) Then
            resultArr(i, 1) = "yes"
            Exit For
        End If
    Next j
Next i

Dim resultRng As Range
Set resultRng = page2.Range("E2:E" & lastrowpage2)
resultRng = resultArr

End Sub
英文:

Adding another option. You mentioned converting the columns to arrays. That is a possibility and should be quite fast. If you get a chance to try it as well as the other answer you got, I'd be interested in the performance difference.

Sub Calculate()

Set page1 = Worksheets("Page1")
Set page2 = Worksheets("Page2")

lastrowpage1 = page1.Cells(page1.Rows.Count, "A").End(xlUp).Row
lastrowpage2 = page2.Cells(page2.Rows.Count, "A").End(xlUp).Row

Dim lookupArr() As Variant, valueArr() As Variant, resultArr() As Variant, rng As Range
Set rng = page1.Range("C2:C" & lastrowpage1)
lookupArr = rng
Set rng = page2.Range("D2:D" & lastrowpage2)
valueArr = rng
ReDim resultArr(1 To UBound(valueArr), 1 To UBound(valueArr, 2))

For i = 1 To UBound(valueArr)
    resultArr(i, 1) = "no"
    For j = 1 To UBound(lookupArr)
        If valueArr(i, 1) = lookupArr(j, 1) Then
            resultArr(i, 1) = "yes"
            Exit For
        End If
    Next j
Next i

Dim resultRng As Range
Set resultRng = page2.Range("E2:E" & lastrowpage2)
resultRng = resultArr

End Sub

huangapple
  • 本文由 发表于 2023年5月22日 20:18:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定