我正在尝试使用 VBA 使用地址函数将公式动态添加到几个单元格中。当我运行以下脚本时,我会打开一个文件资源管理器窗口。我不确定为什么会这样。我究竟做错了什么?
这是我尝试过的解决方案之一:
Sub AddFormulas()
Set countBase = Sheet7.Range("CU2")
colCount = Sheet7.Range(countBase, countBase.End(xlToRight)).Columns.Count
Dim startCount As Integer
startCount = 98
For i = 1 To colCount
If IsNumeric(Sheet7.Cells(2, startCount + i)) Then
Set bSum = Sheet7.Cells(3, colCount + startCount)
Set bSpr = Sheet6.Cells(3, startCount + i)
Sheet7.Cells(3, i).Formula = "=Sheet6!" & bSpr.Address() & "*" & "Sheet7!" & bSpr.Address()
Else
'Do some stuff
End If
Next i
End Sub
我也试过这个:
Sub AddFormulas()
Set countBase = Sheet7.Range("CU2")
colCount = Sheet7.Range(countBase, countBase.End(xlToRight)).Columns.Count
Dim startCount As Integer
startCount = 98
For i = 1 To colCount
If IsNumeric(Sheet7.Cells(2, startCount + i)) Then
Sheet7.Cells(3, i).Formula = "=Sheet6!" & Cells(3, startCount + i).Address() & "*" & "Sheet7!" & Cells(3, colCount + startCount).Address()
Else
'Do some stuff
End If
Next i
End Sub
最佳答案
您是否重命名了工作表?您不能在工作表公式中使用代号,但可以从代号中检索名称。
Sheet7.Cells(3, i).Formula = _
"='" & Sheet6.Name & "'!" & bSpr.Address() & "*'" & Sheet7.Name & "'!" & bSpr.Address()
我在工作表名称中添加了单引号,以防它们包含空格;如果他们不这样做,就没有任何损害。
关于vba - 如何在 Excel VBA 中使用地址功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43100363/