【问题标题】:excel VBA index and match functionsexcel VBA索引和匹配函数
【发布时间】:2017-01-31 16:46:18
【问题描述】:

我正在尝试将 excel 函数更改为 vba 代码。 Col AC,第 2 行中的以下公式... =IF(ROWS($1:1)< MATCH(0.01,H$2:H$10)+1,"",INDEX(X:X,ROWS($1:1)-MATCH(0.01,H$2:H$10)+1))

...扫描 Col H 的前 10 行。

此公式在 Col H 的行中查找第一个非零值。当它找到该行时,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。

当我运行代码时,我得到一个运行时错误 mismatch '13: Type mismatch。

我正在尝试了解之前的问题是如何回答的:Excel VBA: how to solve Index and Match function type mismatch error

【问题讨论】:

  • 您正在使用 MATCH(0.01,H$2:H$10) ,它正在寻找值 0.01,而不是非零值,如果单元格等于 0.2,您的 Match 将不会返回它。您需要将Match 更改为MATCH(0.01,H$2:H$10,-1),第三个参数等于-1 表示它正在寻找大于0.01 的值

标签: excel worksheet-function vba


【解决方案1】:

以前面回答的问题为例。您正在将匹配结果推送到索引公式中。如果匹配结果未找到匹配项,则会返回错误 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

【讨论】:

  • 嗨,Zerk,下面的代码有效。非常感谢您的帮助。
  • 'Dim rw As Variant With Application.WorksheetFunction For i = 2 To lengthRows rw = .Match(0.01, Range("H2:H10")) + 1 '有没有理由你不是为精确匹配指定第三个参数 0? If Not i 结尾
【解决方案2】:

我认为一旦你想使用VBA,你需要使用VBA添加的功能,而不是拘泥于你在Excel中构建的公式。

由于您正在寻找 H 列中第一个具有非零值的单元格,您可以使用 Application.Match 轻松找到它,但您需要将 match 的第三个参数设置为 -1(意味着更大比,寻找值 > 0.01 的匹配项)。

所以现在,我们有了行号,如果你想在 X 列中找到这一行的值,你可以使用Range("AC2").Value = Range("X" &amp; 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

【讨论】:

  • 亲爱的 Shai,当我输入 rw = .Match(0, Range("H2:H10")) + 1 时,它给了我一个具有正确值的 MsgBox。当我使用 rw = .Match(0, Range("H2:H10"), -1) 时,我没有得到正确的值。我在上面尝试了你的 convertFormulaToVBA 代码,它给了我 MsgBox "No non-zero value found at Range " & Rng.Address
  • 您是否复制了我发布的完整代码? h 列中是否有值 > 0.01 的单元格?然后就可以了
  • 我将 0 替换为 0.01,但它对我不起作用。我将您的代码与 Zerk 的代码结合起来,以获得下面的代码。非常感谢您的帮助!
  • Dim rw As Variant With Application.WorksheetFunction For i = 2 To lengthRows rw = .Match(0.01, Range("H2:H10")) + 1 '有没有你没有指定的原因精确匹配的第三个参数 0? If Not i
猜你喜欢
  • 2021-04-04
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2020-07-05
  • 2011-11-26
  • 1970-01-01
相关资源
最近更新 更多