VBA公式中的dynamic参考

我试图索引/匹配数据只有当一定的标准得到满足。

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

我的代码如下:

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 

然后将该子文件传回主程序。

我试图让dynamic是'H113'单元格。 因为它已经在公式中,所以我似乎无法得到一个正确的工作。

编辑:道歉,H113下移。 下一个单元格将是H114。

问候

请试试这个代码。

 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 

知道你想去H113,H114:

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