在VBA编码错误中编写Excel公式" 1004"

时间:2017-08-22 12:32:35

标签: excel vba excel-vba

我一直得到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代码?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

点击 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

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