1

I keep getting Excel VBA Error "1004" on executing the following formula..

Click to see the Excel Formula Solution

..in VBA Code

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

Does anyone know how to make sure to write the VBA Code correctly?

2 Answers 2

1

If i was 1 your formula resolves to

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

You are missing the & in the formula.

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
Sign up to request clarification or add additional context in comments.

Comments

0

Click on the formula cell in N5, then run this code:

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

Take a look at the immediate window and fix your formula correspondingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.