我有一份预测数据电子表格,我需要不断更新预测单元格。手动执行此操作需要数小时(数百个单元格)。 我正在尝试编写一个在每个单元格中编写公式的宏,但我仍然坚持语法。这是我的原型。
问题:如何从“预测”标签中正确循环显示单元格?
Sub forecastBuild()
Dim rng As Range, i As Range, colCount As Integer, col As Integer
colCount = 14
Set rng = Range("D6, H6, L6, P6, U6, Y6, AC6, AH6, AL6, AQ6, AU6, AY6")
For Each i In rng
For col = 3 To colCount
i.Formula = "=Forecast!$" & columns(, col) & "$7"
Next col
Next i
End Sub
rng
是我的输出范围(目前要编写公式),我正在从Forecast!C7:Forecast!N7
获取数据。我不相信columns(, col)
语法是正确的,并且VBA没有将整数转换为字符串。感谢。
答案 0 :(得分:2)
我认为这就是你想要的:
Sub forecastBuild()
Dim rng As Range, i As Range, colCount As Integer, col As Integer
Dim ws As Worksheet
Set ws = ActiveSheet
Set rng = ws.Range("D6, H6, L6, P6, U6, Y6, AC6, AH6, AL6, AQ6, AU6, AY6")
colCount = 3
For Each i In rng
i.Formula = "=" & Sheets("Forecast").Cells(7, colCount).Address(0, 0, xlA1, True)
colCount = colCount + 1
Next i
End Sub
答案 1 :(得分:1)
我不太确定我是否理解正确。但也许你正在寻找一条沿着这条线的东西:
Option Explicit
Sub forecastBuild()
Dim rng As Range, i As Range, colCount As Integer
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("D6, H6, L6, P6, U6, Y6, AC6, AH6, AL6, AQ6, AU6, AY6")
For Each i In rng
i.Formula = "=Forecast!" & Cells(7, 3 + colCount).Address
colCount = colCount + 1
Next i
End Sub