VBA代码中的索引函数

时间:2018-03-09 19:37:28

标签: excel vba indexing

有人可以帮助我在VBA中转换此代码。 可以通过工作表功能来实现。 对不起,我是一个Noob,只是在学习。

=INDEX($L$1:$Z$4,4,MATCH(AQ$2,$L$1:$Z$1,0))

2 个答案:

答案 0 :(得分:1)

类似于以下内容(或使用评估),但此句柄未找到。

Sub TEST()

Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1") ' change

Dim indexRange As Range
Set indexRange = ws.Range("$L$1:$Z$4")

Dim matchRange As Range
Set matchRange = ws.Range("$L$1:$Z$1")
Dim searchValue As Variant

searchValue = ws.Range("AQ$2").Value2

Dim result As Variant

On Error GoTo errHand
result = Application.WorksheetFunction.Index(indexRange, 4, Application.WorksheetFunction.Match(searchValue, matchRange, 0))

MsgBox result

Exit Sub

errHand:

If Err.Number <> 0 Then
    MsgBox "Value not found"
End If

End Sub

编辑:

或者根据@ScottCraner:简化

result = Application.WorksheetFunction.Index(indexRange, 4, Application.WorksheetFunction.Match(searchValue, matchRange, 0))

result = indexRange.Cells(4,Application.WorksheetFunction.Match(searchValue, matchRange, 0))

答案 1 :(得分:0)

容易!

MsgBox Evaluate("=INDEX($L$1:$Z$4,4,MATCH(AQ$2,$L$1:$Z$1,0))")

更多信息: