索引匹配功能:遍历多个条件

时间:2020-05-08 10:19:45

标签: excel vba

我最初的帖子中的内容,其中我没有提供我想要做什么的详细描述。

我想获取一个匹配两个条件的单元格的值。一个条件是修复单元,而第二个条件是我需要对其进行迭代。

示例:

Cells(2,1) = A(它与“名称”和Criteria2 (Cells(1,6))匹配,对于Cells(3,1),我想应用相同的内容,但要使用Criteria2 (Cells(2,6)),依此类推。

我的替代方法是过滤Range Criteria 1(我已经做过),然后获取所有可用值,但是我必须对3个不同的集合应用相同的过程,因此我觉得可以采用更好的方法匹配/索引。

下面您可以找到我的Excel文件,代码和输出。

enter image description here

这是我的代码的输出:

enter image description here

这是我的代码:

Sub match_index()

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

Dim i As Long
i = 2

Dim criteria1 As String
criteria1 = ws.Cells(1, 4).Value
Dim criteria2 As String
criteria2 = ws.Cells(1, 6).Value

Dim criteria_range1 As Range
Set criteria_range1 = ws.Range(ws.Cells(2, 8), ws.Cells(14, 8))
Dim criteria_range2 As Range
Set criteria_range2 = ws.Range(ws.Cells(2, 9), ws.Cells(14, 9))


With Application
While i < 8
        ws.Cells(i, 1).Value = .Index(criteria_range2, _
        .Match(criteria1, criteria_range1, 0), _
        .Match(ws.Cells(i, 6).Value, criteria_range2, 0), 1)
        i = i + 1
Wend
End With

End Sub

0 个答案:

没有答案