【问题标题】:ExecuteExcel4Macro - Run-time error 1004 "The formula you typed contains an error."ExecuteExcel4Macro - 运行时错误 1004“您键入的公式包含错误。”
【发布时间】:2016-01-13 13:41:09
【问题描述】:

运行时错误 1004“您键入的公式包含错误。”

Private Sub UserForm_Initialize()
Dim wbPath, wbName, wsName, cellRef, Ret As String
    Set xlApp = CreateObject("Excel.application")
    wbPath = "N:\BVN\1W\02 GpDS\04 Sp\03 Horeca\04 SBC\02 Private\01 Boekhouding sbc\"
    wbName = "TEGOEDEN-AVOIR.xls"
    wsName = "CIS-A4"
    cellRef = "F6"
    Ret = "'" & wbPath & "[" & wbName & "]" & wsName & "'!" & Range(cellRef).Address(True, True)
    Ret = CStr(Ret)
    Me.Label11.Caption = xlApp.ExecuteExcel4Macro(Ret)
End Sub

我在变量上添加了一个手表,看起来没问题:

"'N:\BVN\1W\02 GpDS\04 Sp\03 Horeca\04 SBC\02 Private\01 Boekhouding sbc\[TEGOEDEN-AVOIR.xls]CIS-A4'!$F$6"

我在这里错过了什么?

【问题讨论】:

    标签: excel vba excel-4.0


    【解决方案1】:

    唯一的错误是您需要使用 R1C1 引用:

    Ret = "'" & wbPath & "[" & wbName & "]" & wsName & "'!" & Range(cellRef).address(True, True, xlR1C1)
    

    会起作用(当然,只要其余部分正确!)

    【讨论】:

      【解决方案2】:

      路径属于工作簿文件名,前面不分开:

      另一个提示(尚未导致错误):this

      Dim wbPath, wbName, wsName, cellRef, Ret As String
      

      将定义 4 个Variants 和一个string。你真正想要的是

      Dim wbPath As String, wbName As String, wsName As String, cellRef As String, Ret As String
      

      【讨论】:

      • 恐怕这不是真的。路径属于它在原始代码中的位置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多