2

I've made what i thought is a simple script in VBA that performs the specified formula and applies it all the way down column D. When i run it i get a Runtime Error 13 Type mismatch. Ive narrowed it down the actual formula part by replacing the formula with something simple like 1+1 and then it works fine. Any advise?

Type Mismatch error on:

Sub FillFormula()
Range("D2").Formula = "=SUBSTITUTE(D2;" - ";"")"
Range("D2", "D" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
End Sub

Works fine with:

Sub FillFormula()
Range("D2").Formula = "=1+1"
Range("D2", "D" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
End Sub
5
  • 1
    Use commas rather than semi-colons in VBA. Commented Nov 7, 2017 at 14:34
  • 2
    Quotes in quotes must be double quotes to quote them as quotes: "=SUBSTITUTE(D2, "" - "", """")" Commented Nov 7, 2017 at 14:36
  • @SJR Correct me if I'm wrong, but I believe there are some European versions of Excel that use ; instead of , Commented Nov 7, 2017 at 14:55
  • 1
    @Maldred - in spreadsheets, but I think VBA always uses commas (though could be wrong), although an alternative is to use FormulaLocal. Commented Nov 7, 2017 at 15:01
  • @AlexK. Well said. :) Commented Nov 7, 2017 at 15:45

2 Answers 2

1
  • Write the Excel formula on D2 the way you want it to be and make sure it works in Excel.
  • Select D2 and run the following:

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

Use whatever is shown in the immediate window. It should work. I have been using it quite a lot.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to all. Used advice from everyone and in no time had it working. Final Formula:

Sub FillFormula()
Range("F2").Formula = "=SUBSTITUTE(D2,""-"","""")"
Range("F2", "F" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
End Sub

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.