为了通过VBA获得动态公式,我不得不使用INDIRECT(ADDRESS(。我现在已经使用了几十个看起来像这样的公式:
=INDIRECT(ADDRESS(37,10),1)*(INDIRECT(ADDRESS(37,7),1)+(INDIRECT(ADDRESS(69,10),1)*INDIRECT(ADDRESS(77,10),1))+(INDIRECT(ADDRESS(70,10),1)*INDIRECT(ADDRESS(78,10),1))+(INDIRECT(ADDRESS(71,10),1)*INDIRECT(ADDRESS(79,10),1))+(INDIRECT(ADDRESS(72,10),1)*INDIRECT(ADDRESS(80,10),1))+(INDIRECT(ADDRESS(73,10),1)*INDIRECT(ADDRESS(81,10),1))+(INDIRECT(ADDRESS(74,10),1)*INDIRECT(ADDRESS(82,10),1))+(INDIRECT(ADDRESS(75,10),1)*INDIRECT(ADDRESS(83,10),1)))
我不能将这些公式与Solver一起使用,因为它对Solver来说太过分了。当我手动将公式转换为" A1"格式,然后解决方案工作。我需要自动化翻译,因为这是一个模板。
我希望更换" INDIRECT(地址"" Cells"可能会有效。没有运气。有没有我可以使用的表达可以取代" INDIRECT(ADDRESS& #34;会读取R1C1格式吗?或者你有更好的解决方案吗?
谢谢!
r = 0
Do Until r = 6
i = 0
Do Until i = DCShipFrom
Cells(70 + r, 7 + DCShipFrom + i).Select
Selection.FormulaR1C1 = "=IF(AND(INDIRECT(ADDRESS(" & 67 & "," & 7 + DCShipFrom + i & "),1)<=" & "INDIRECT(ADDRESS(" & 70 + r & "," & 6 & "),1)," & "INDIRECT(ADDRESS(" & 67 & "," & 7 + DCShipFrom + i & "),1)>" & "INDIRECT(ADDRESS(" & 69 + r & "," & 6 & "),1)),1,0)"
i = i + 1
Loop
r = r + 1
Loop
enter code here
答案 0 :(得分:0)
使用ADDRESS(37, 10)
返回$ J $ 37使用,
INDIRECT(ADDRESS(37, 10), 1) 'or INDIRECT(ADDRESS(37,10)) since xlA1 is default
使用ADDRESS(37, 10, 1, 0)
返回R37C10,
INDIRECT(ADDRESS(37, 10, 1, 0), 0) 'the zeroes denote xlR1C1 addressing
您可能需要考虑以下其中一项,
INDEX($J:$J, 37)
INDEX($A:$Z, 37, 10)
INDEX是非易失性的。 INDIRECT和ADDRESS都是不稳定的。
答案 1 :(得分:0)
您可以将INDIRECT与R1C1格式一起使用,在间接语句的末尾添加FALSE,并使其变为动态,例如,创建行和列变量。
INDIRECT("R" & myrow & "C" & mycol,FALSE)
答案 2 :(得分:0)
试试这个,它使公式动态,但更简单:
For r = 0 To 6
For i = 0 To DCShipFrom
With ActiveSheet
.Cells(70 + r, 7 + DCShipFrom + i).Formula = "=IF(AND(" & .Cells(67, 7 + DCShipFrom + i).Address(0, 0) & "<=" & .Cells(70 + r, 6).Address(0, 0) & "," & .Cells(67, 7 + DCShipFrom + i).Address & " >" & .Cells(69 + r, 6).Address & "),1,0)"
End With
Next i
next r
或者如果你想要R1C1:
For r = 0 To 6
For i = 0 To DCShipFrom
With ActiveSheet
.Cells(70 + r, 7 + DCShipFrom + i).FormulaR1C1 = "=IF(AND(R" & 67 & "C" & 7 + DCShipFrom + i & " <= R" & 70 + r & "C" & 6 & ",R" & 67 & "C" & 7 + DCShipFrom + i & " > R" & 69 + r & "C" & 6 & "),1,0)"
End With
Next i
Next r