Dim worksheet1 As Worksheet
Set worksheet1 = ActiveSheet
Dim workbook2 As Workbook
Set workbook2 = Workbooks.Open(("F:\Project Sweep\Kim Checklist\" & worksheet1.Cells(19, 12) & "\Consumers.xlsx"))
Dim targetString As String
targetString = "index('" & workbook2.Path & "\[" & workbook2.Name & "]Time'!$A$1:$E$366, 1, 1)"
Dim i As Long
For i = 0 To 4
worksheet1.Cells(7 + (i * 2), 7) = Application.Calculate(targetString)
Next i
代码正在尝试执行以下操作:
我使用的另一个选项是
worksheet1.Range(Cells(7 + (i * 2), 7)).Formula = "=" & targetString
实际上只是将公式放在单元格中。但这会产生错误“运行时错误1004:对象的方法范围失败”
任何帮助将不胜感激,谢谢您的宝贵时间。
答案 0 :(得分:1)
您要查找Application.Evaluate,而不是Application.Calculate。
For i = 0 To 4
worksheet1.Cells(7 + (i * 2), 7) = Application.Evaluate(targetString)
Next i
由于循环中看起来像公式的字符串始终是静态的,因此我不清楚在该循环期间将发生什么变化。
使用.Address和External:= True来解析完整的外部路径,工作簿,工作表和范围。
with workbook2.worksheets("Time").range("A1:E366")
targetString = "index(" & .address(external:=true) & ", 1, 1)"
debug.print targetString
end with