excel VBA 索引和匹配函数
excel VBA index and match functions
我正在尝试将 excel 函数更改为 vba 代码。下面的公式在 Col AC,第 2 行......
=IF(ROWS(:1)< MATCH(0.01,H:H)+1,"",INDEX(X:X,ROWS(:1)-MATCH(0.01,H:H)+1))
...扫描 Col H 的前 10 行。
此公式在 Col H 的行中查找第一个 none-零值。当它找到该行时,col X 中的值将在 Col AC 中打印出来,以便Col AC 匹配 Col H 中第一个非零值的行。
我希望描述有意义。它在 excel 工作表中完美运行。现在,我想将其更改为 VBA 代码,这就是我所拥有的...
For i = 2 To lengthRows
With Application.WorksheetFunction
Range("AC" & i) = .IF(Rows(1) < .Match(0.01, Range("H2:H10")) + 1, "", .Index(Columns(24), Rows(1) - .Match(0.01, Range("H2:H10")) + 1))
End With
Next i
...Rows(1) 是第一行,Columns(24) 是 Col X。
当我 运行 代码时,我得到 运行 时间错误不匹配 '13:类型不匹配。
我想了解上一个问题是如何回答的:
以之前回答的问题为例。您正在将匹配结果推入索引公式。如果匹配结果未找到匹配项,则会出现 return 错误 2042,将其推入索引公式后会出现不匹配错误。
为您的示例调整该解决方案如下:
Dim rw As Variant
With Application.WorksheetFunction
For i = 2 To lengthRows
rw = .Match(0.01, Range("H2:H10")) 'Is there a reason you're not specifying the third parameter 0 for exact match?
If Not IsError(rw) Then
Range("AC" & i) = .If(Rows(1) < .Match(0.01, Range("H2:H10")) + 1, "", .Index(Columns(24), Rows(1) - .Match(0.01, Range("H2:H10")) + 1))
Else
' Do something else if there was an error
End If
Next i
End With
我认为一旦你想使用VBA,你需要使用VBA添加的功能,而不是坚持你在Excel中构建的公式。
由于您正在寻找 H 列中第一个具有 none-zero 值的单元格,您可以使用 Application.Match
轻松找到它,但是您需要将 match 的第三个参数设置为-1
(表示大于,寻找值 > 0.01 的匹配项)。
现在,我们有了行号,如果您想在该行的 X 列中查找值,可以使用 Range("AC2").Value = Range("X" & MatchRow + Rng.Item(0).Row).Value
代码
Option Explicit
Sub ConvertFormulaToVBA()
Dim MatchRow As Variant
Dim Rng As Range
Dim lengthRows As Long, i As Long
lengthRows = Cells(Rows.Count, "H").End(xlUp).Row '<-- get last row with data in Column H (in your example it's 10)
Set Rng = Range("H2:H" & lengthRows) ' <-- set the range to H2 until last row in Column H
MatchRow = Application.Match(0.01, Rng, -1) ' <-- setting the third parameter to -1, meaning greater than 0.01
If Not IsError(MatchRow) Then
Range("AC2").Value = Range("X" & MatchRow + Rng.Item(0).Row).Value
Else
' raise a message box if there is no Match
MsgBox "No none-zero value found at Range " & Rng.Address
End If
End Sub
我正在尝试将 excel 函数更改为 vba 代码。下面的公式在 Col AC,第 2 行......
=IF(ROWS(:1)< MATCH(0.01,H:H)+1,"",INDEX(X:X,ROWS(:1)-MATCH(0.01,H:H)+1))
...扫描 Col H 的前 10 行。
此公式在 Col H 的行中查找第一个 none-零值。当它找到该行时,col X 中的值将在 Col AC 中打印出来,以便Col AC 匹配 Col H 中第一个非零值的行。
我希望描述有意义。它在 excel 工作表中完美运行。现在,我想将其更改为 VBA 代码,这就是我所拥有的...
For i = 2 To lengthRows
With Application.WorksheetFunction
Range("AC" & i) = .IF(Rows(1) < .Match(0.01, Range("H2:H10")) + 1, "", .Index(Columns(24), Rows(1) - .Match(0.01, Range("H2:H10")) + 1))
End With
Next i
...Rows(1) 是第一行,Columns(24) 是 Col X。
当我 运行 代码时,我得到 运行 时间错误不匹配 '13:类型不匹配。
我想了解上一个问题是如何回答的:
以之前回答的问题为例。您正在将匹配结果推入索引公式。如果匹配结果未找到匹配项,则会出现 return 错误 2042,将其推入索引公式后会出现不匹配错误。
为您的示例调整该解决方案如下:
Dim rw As Variant
With Application.WorksheetFunction
For i = 2 To lengthRows
rw = .Match(0.01, Range("H2:H10")) 'Is there a reason you're not specifying the third parameter 0 for exact match?
If Not IsError(rw) Then
Range("AC" & i) = .If(Rows(1) < .Match(0.01, Range("H2:H10")) + 1, "", .Index(Columns(24), Rows(1) - .Match(0.01, Range("H2:H10")) + 1))
Else
' Do something else if there was an error
End If
Next i
End With
我认为一旦你想使用VBA,你需要使用VBA添加的功能,而不是坚持你在Excel中构建的公式。
由于您正在寻找 H 列中第一个具有 none-zero 值的单元格,您可以使用 Application.Match
轻松找到它,但是您需要将 match 的第三个参数设置为-1
(表示大于,寻找值 > 0.01 的匹配项)。
现在,我们有了行号,如果您想在该行的 X 列中查找值,可以使用 Range("AC2").Value = Range("X" & MatchRow + Rng.Item(0).Row).Value
代码
Option Explicit
Sub ConvertFormulaToVBA()
Dim MatchRow As Variant
Dim Rng As Range
Dim lengthRows As Long, i As Long
lengthRows = Cells(Rows.Count, "H").End(xlUp).Row '<-- get last row with data in Column H (in your example it's 10)
Set Rng = Range("H2:H" & lengthRows) ' <-- set the range to H2 until last row in Column H
MatchRow = Application.Match(0.01, Rng, -1) ' <-- setting the third parameter to -1, meaning greater than 0.01
If Not IsError(MatchRow) Then
Range("AC2").Value = Range("X" & MatchRow + Rng.Item(0).Row).Value
Else
' raise a message box if there is no Match
MsgBox "No none-zero value found at Range " & Rng.Address
End If
End Sub