How to pass variables to an double match function in VBA

后端 未结 2 1859
独厮守ぢ
独厮守ぢ 2021-01-23 15:08

I have a bunch of rows and 25 columns in a worksheet, and need to find the value in the 4th column based on columns B and C using VBA. I am using a combination of index and mult

相关标签:
2条回答
  • 2021-01-23 15:21

    If I understand correctly, I think your syntax was just off slightly - you omitted some ampersands and overlooked the rule about doubling up the quotes. Also no need to Select.

    Range("G27").FormulaArray = "=index(A2:G27,match(1,(B2:B27=""" & SA & """)*(C2:C27=""" & C1 & """),0),4)"
    
    0 讨论(0)
  • 2021-01-23 15:24

    May be a bit quicker using memory arrays:

    Sub VariablesInArrayFormula()
    Dim SA As String
    SA = "Apples"
    
    Dim C1 As String
    C1 = "Oranges"
    
    With Worksheets("Sheet1") 'Change to your worksheet
        Dim DtaArray As Variant
        DtaArray = .Range("B2:D27").Value
    
        Dim i As Long
        For i = LBound(DtaArray, 1) To UBound(DtaArray, 1)
            Dim ans
            If DtaArray(i, 1) = SA And dtaaray(i, 2) = C1 Then
                ans = DtaArray(i, 3)
                Exit For
            End If
        Next i
    
        .Range("G1").Value = ans
    End With
    
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题