Excel 在具有可变查找位置的VBA中使用索引匹配

Excel 在具有可变查找位置的VBA中使用索引匹配,excel,vba,Excel,Vba,在索引匹配中使用查找条件内的变量时遇到问题。一些背景:我使用以下代码将变量的值设置为B列中包含“Current”的任何单元格的行: Dim rowHeaderNum As Integer rowHeaderNum = 0 On Error Resume Next rowHeaderNum = Application.Match("Current", ActiveSheet.Range("B:B"), 0) On Error GoTo 0 然后,我使用以下命

在索引匹配中使用查找条件内的变量时遇到问题。一些背景:我使用以下代码将变量的值设置为B列中包含“Current”的任何单元格的行:

Dim rowHeaderNum As Integer

    rowHeaderNum = 0

    On Error Resume Next
    rowHeaderNum = Application.Match("Current", ActiveSheet.Range("B:B"), 0)
    On Error GoTo 0

然后,我使用以下命令将包含值“CurrentActual”的行“rowHeaderNum”中单元格的列#存储到另一个变量:

Dim currActColNum As Integer

currActColNum = 0
    currActColNum = Application.Match("CurrentActual", Rows(rowHeaderNum & ":" & rowHeaderNum), 0)

下面是我无法工作的索引匹配线:

Dim currActRev As Double

    currActRev = Application.Index(Columns(currActColNum), Application.Match("Gross Operating Profit", Columns("N:N"), 0))

currActRev
将存储美元金额。
Match
函数将始终使用列N作为查找数组。当我运行
索引
匹配
行时,我得到一个

类型不匹配

调试器出错。

使用
工作表函数…
Application.Match
错误恢复下一步
不起作用,因为
Application.Match
不会引发异常,您需要使用
WorksheetFunction.Match

根据文档,它返回一个
Double
,因此您需要
将RowHeaderNum设置为Double

Dim RowHeaderNum As Double
'RowHeaderNum = 0 'not needed it is always 0 after dim

On Error Resume Next
RowHeaderNum = Application.WorksheetFunction.Match("Current", ActiveSheet.Range("B:B"), False)
On Error GoTo 0
此外,您需要检查
RowHeaderNum
是否为
0
,并停止进行,否则以下代码将失败,因为行
0
不存在

If RowHeaderNum = 0 Then
    MsgBox "'Current' not found."
    Exit Sub
End If
你需要在这里做同样的事情

Dim CurrActColNum As Double
On Error Resume Next
CurrActColNum = Application.WorksheetFunction.Match("CurrentActual", Rows(RowHeaderNum), False)
On Error GoTo 0

If CurrActColNum = 0 Then
    MsgBox "'CurrentActual' not found."
    Exit Sub
End If
最后,返回的是
变量
而不是
双精度
,您也需要在此处进行错误处理

Dim currActRev As Variant
On Error Resume Next
currActRev = Application.WorksheetFunction.Index(Columns(CurrActColNum), Application.WorksheetFunction.Match("Gross Operating Profit", Columns("N:N"), False))
On Error GoTo 0

Debug.Print currActRev 'print result in immediate window

使用
应用程序…
请注意,您也可以使用
应用程序.Match
应用程序.Index
(不带
工作表功能
),但是您不能在出现错误时使用
,您必须使用
iError()
检查错误。此外,您的变量需要声明为
变量
,因为
应用程序.Match
可以返回打字错误
Double
或类型
错误

Dim RowHeaderNum As Variant
RowHeaderNum = Application.Match("Current", ActiveSheet.Range("B:B"), False)

If IsError(RowHeaderNum) Then
    MsgBox "'Current' not found."
    Exit Sub
End If

Dim CurrActColNum As Variant
CurrActColNum = Application.Match("CurrentActual", Rows(RowHeaderNum), False)

If IsError(CurrActColNum) Then
    MsgBox "'CurrentActual' not found."
    Exit Sub
End If


Dim currActRev As Variant, currMatch As Variant
currMatch = Application.Match("Gross Operating Profit", Columns("N:N"), False)
If Not IsError(currMatch) Then
    currActRev = Application.Index(Columns(CurrActColNum), currMatch)
End If

Debug.Print currActRev 'print result in immediate window

应用程序是否有索引或匹配方法?@siggi\u pop是的,它们存在。就像其他人回答的那样,您可能应该使用WorksheetFunction提供的正确方法
WorksheetFunction.Match
WorksheetFunction.Index
@Pᴇʜ我使用
工作表函数
,因为在我的Excel版本中,我无法访问
应用程序。Index
@siggi_pop
应用程序。Match
工作表函数。Match
都是正确的,并且都存在于Excel中(第一个没有intelli sense工具提示)。它们只是在处理错误的方式上有所不同。当
WorksheetFunction
抛出一个异常,该异常可以通过
On Error…
捕获时,
应用程序
函数不会执行一个异常,而是返回一个错误代码作为结果。请参阅下面我编辑的答案,以了解如何使用这两个选项。