【问题标题】:Dynamic Referencing in VBA FormulaVBA 公式中的动态引用
【发布时间】:2018-03-29 18:03:26
【问题描述】:

我仅在满足特定条件时才尝试索引/匹配数据。

我可以用两个数组来做到这一点,但我希望这里有一个简单的答案。

我的代码如下:

Sub Nozeroleftbehind(lengthRow As Integer)
For i = 2 To lengthRow
    If Cells(1, i) = 0 Then Cells(1, i) = "TBD"
Next i

For i = 2 To lengthRow
    If Cells(1, i) = "#N/A" Then
        Cells(2, i) = "=INDEX(Forecast!L:L,MATCH('AA - Inbound Orders Weekly Rep'!H113,Forecast!A:A,0))"
End if
Next i

    End Sub

然后将该子程序传递回主程序。

我想要动态的是“H113”单元格。我似乎无法让偏移量正常工作,因为它已经在公式中。

编辑:抱歉,H113 向下移动。下一个单元格是 H114。

问候

【问题讨论】:

  • 请澄清:如果 H113 是“动态的”,随着循环的进行,它会是什么样子? I113、J113 还是 H114、H115...?
  • H114。很抱歉没有包括在内。

标签: vba excel offset


【解决方案1】:

请试试这个代码。

Sub NoZeroLeftBehind(lengthRow As Integer)
    ' 18 Oct 2017

    Dim lengthRow As Long
    Dim Tmp As Variant
    Dim C As Long

    lengthRow = 4
    For C = 2 To lengthRow
        ' bear in mind that the Cell is a Range
        ' and you want to refer to its Value & Formula property
        With Cells(1, C)
            Tmp = .Value
            ' using the Val() function will interpret a blank cell as zero value
            If Val(Tmp) = 0 Then
                .Value = "TBD"
            ElseIf IsError(Tmp) Then
                .Formula = "=INDEX(Forecast!L:L,MATCH('AA - Inbound Orders Weekly Rep'!H" & _
                           (113 + C - 2) & ",Forecast!A:A,0))"
            End If
        End With
    Next C
End Sub

【讨论】:

  • 谢谢!不得不稍微改变它,但除此之外,它工作得很好!非常感谢。
【解决方案2】:

知道你想去H113,H114:

Cells(2, i) = "=INDEX(Forecast!L:L,MATCH('AA - Inbound Orders Weekly Rep'!H" & CStr(111 + i) & ",Forecast!A:A,0))"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2018-10-15
    • 2017-10-15
    相关资源
    最近更新 更多