【问题标题】:Write Excel Formula in VBA Coding Error "1004"在 VBA 编码错误“1004”中编写 Excel 公式
【发布时间】:2017-08-22 20:42:56
【问题描述】:

执行以下公式时,我不断收到 Excel VBA 错误“1004”..

Click to see the Excel Formula Solution

..在 VBA 代码中

For i = 5 To lastrow
      ws2.Cells(i, lastcolumn + 1).Formula = "=DATEVALUE(INDIRECT(LEFT($F$1,1)" & i & "))+TIMEVALUE(INDIRECT(LEFT($H$1,1)" & i & "))"
      ws2.Cells(i, lastcolumn + 1).NumberFormat = "dd.mm.yyyy hh:mm:ss"
Next i

有人知道如何确保正确编写 VBA 代码吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果 i 为 1,您的公式将解析为

    =DATEVALUE(INDIRECT(LEFT($F$1,1)1)) + +TIMEVALUE(INDIRECT(LEFT($H$1,1)1))
    

    公式中缺少&

    For i = 5 To lastrow
          ws2.Cells(i, lastcolumn + 1).Formula = "=DATEVALUE(INDIRECT(LEFT($F$1,1) & " & i & "))+TIMEVALUE(INDIRECT(LEFT($H$1,1) & " & i & "))"
          ws2.Cells(i, lastcolumn + 1).NumberFormat = "dd.mm.yyyy hh:mm:ss"
    Next i
    

    【讨论】:

      【解决方案2】:

      点击N5中的公式单元格,然后 运行这段代码:

      Public Sub PrintMeUsefulFormula()
      
          Dim strFormula  As String
          Dim strParenth  As String
      
          strParenth = """"
      
          strFormula = Selection.Formula
          strFormula = Replace(strFormula, """", """""")
      
          strFormula = strParenth & strFormula & strParenth
          Debug.Print strFormula
      
      End Sub
      

      查看即时窗口并相应地修复您的公式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多