使用VBA在每个其他单元格中填写公式

时间:2015-10-01 12:53:51

标签: excel vba

我需要每隔一列都有一个网络网址来收集来自不同.xlsx文件的值。我的代码使用简单的公式(即"=1+5")填充正确的单元格,但不填充网络网址字符串myString。我收到了错误

  

运行时错误' 1004':应用程序定义或对象定义的错误

Cells(thisRow, 2 * i).Formula = myString & a & "$23"行。请参阅下面的示例代码

Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function

Sub fillThePaths()
    Dim Filepath, Filename, myString, a As String
    Dim i, thisRow, startColumn, startYear, spanYear As Integer

    Filepath = "\\myPath\"
    Filename = "myFile.xlsm"
    thisRow = 3
    startColumn = 5
    startYear = 2018
    spanYear = 2040 - startYear
    myString = "='" & Filepath & "[" & Filename & "]Totalresultat'!"

    For i = 1 To spanYear
        a = Col_Letter(i + 2)
        Cells(thisRow, 2 * i).Formula = myString & a & "$23"
    Next i
End Sub

0 个答案:

没有答案