gpt4 book ai didi

excel - VBA(Excel): Find Based on Multiple Search Criteria Without Looping

转载 作者:行者123 更新时间:2023-12-02 00:54:16 25 4
gpt4 key购买 nike

我有一个大型数据表,我想根据 3 组条件在 VBA 中进行搜索。可以假设每个行条目是唯一的。表格/数据本身的格式不能因要求而改变。 (我看过几篇有关相关问题的帖子,但尚未找到有效的解决方案。)

一开始我用的是经典的VBA find循环中的方法:

Set foundItem = itemRange.Find(What:=itemName, Lookin:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows)
If Not foundItem Is Nothing Then
firstMatchAddr = foundItem.Address
Do
' *Check the other fields in this row for a match and exit if found*
Set foundItem = itemRange.FindNext(foundItem)
Loop While foundItem.Address <> firstMatchAddr And Not foundItem Is Nothing
End If

但是由于需要对大量数据调用多次,因此速度并不好。

我做了一些搜索,发现我可以使用 match方法 index 。所以我尝试了很多变体,但没有成功,例如:

result = Evaluate("=MATCH(1, (""" & criteria1Name & """=A2:A" & lastRow & ")*(""" & criteria2Name & """=B2:B" & lastRow & ")*(""" & criteria3Name & """=C2:C" & lastRow & "), 0)")

还有

result = Application.WorksheetFunction.Index(resultRange, Application.WorksheetFunction.Match((criteria1Name = criteria1Range)*(criteria2Name = criteria2Range)*(criteria3Name = criteria3Range))

还有

result = Application.WorksheetFunction.Index(resultRange, Application.WorksheetFunction.Match((criteria1Range=criteria1Name )*(criteria2Range=criteria2Name )*(criteria3Range=criteria3Name ))

然后我尝试使用 AutoFilter排序:

.Range(.Cells(1,1), .Cells(lastRow, lastCol)).AutoFilter Field:=1, Criteria1:="=" & criteria1Name
.Range(.Cells(1,1), .Cells(lastRow, lastCol)).AutoFilter Field:=2, Criteria1:="=" & criteria2Name
.Range(.Cells(1,1), .Cells(lastRow, lastCol)).AutoFilter Field:=3, Criteria1:="=" & criteria3Name

但由于其中一个排序列包含日期,因此我在使自动筛选正常工作时遇到问题。

我的问题是,如何根据多个条件搜索 Excel VBA 中的列,不循环,返回行号或我所在行的单元格中的值我感兴趣

最佳答案

您可以使用高级过滤器。将列标题放在工作表的单独部分(或完全不同的工作表)中。在这些列标题下,将您要在每列中查找的条件放入其中。然后将该范围(包括标题)命名为“Criteria”之类的名称。那么宏就变成了:

Sub Macro1()

Sheets("Sheet1").Range("A1").CurrentRegion.AdvancedFilter xlFilterInPlace, Range("Criteria")

End Sub

作为我下面评论的后续内容,让 VBA 在幕后创建标准范围:

Sub Macro1()

'Code up here that defines the criteria

Application.ScreenUpdating = False
Application.DisplayAlerts = False

With Sheets.Add
'Create the advanced filter criteria range
.Range("A1") = "HeaderA"
.Range("B1") = "HeaderB"
.Range("C1") = "HeaderC"
.Range("A2") = criteria1Name
.Range("B2") = criteria2Name
.Range("C2") = criteria3Name

'Alternately, to save space:
'.Range("A1:C1").Value = Array("HeaderA", "HeaderB", "HeaderC")
'.Range("A2:C2").Value = Array(criteria1Name, criteria2Name, criteria3Name)

'Then perform the advanced filter
Sheets("Sheet1").Range("A1").CurrentRegion.AdvancedFilter xlFilterInPlace, .Range("A1:C2")

'Remove behind the scenes sheet now that the filter is completed
.Delete
End With

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

关于excel - VBA(Excel): Find Based on Multiple Search Criteria Without Looping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com