如何在Excel VBA中使用地址function?

我正在尝试使用VBAdynamic地将公式添加到使用地址函数的几个单元格中。 当我运行以下脚本时,将会打开一个文件资源pipe理器窗口。 我不知道为什么会发生这种情况。 我究竟做错了什么?

这是我尝试的解决scheme之一:

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() 

如果包含空格,我已经将单引号添加到工作表名称中; 如果他们不这样做就没有什么不利的。

我会去如下

 With Sheet7 With .Range("CU2") colCount = .Range(.Cells, .End(xlToRight)).Columns.Count End With startCount = 98 For i = 1 To colCount If IsNumeric(.Cells(2, startCount + i)) Then Set bSum = .Cells(3, colCount + startCount) Set bSpr = Sheet6.Cells(3, startCount + i) .Cells(3, i).Formula = "=" & Sheet6.Name & "!" & bSpr.Address() & "*" & .Name & "!" & bSpr.Address() Else 'Do some stuff End If Next i End With