Forum Discussion

JoeCavasin's avatar
JoeCavasin
Brass Contributor
Jan 18, 2025

Adding COUNTIF formula to cell by VBA code

Morning all, need some assistance.  Attempting to insert a new formula by VBA with an already working Macro.  All the other references unhighlighted below work perfectly, but I am missing something with the syntax around a countif function. 

 

The countif currently works on the excel sheet itself, however adding it into the VBA is generating the typical "Compile error: expected: end of statement" error indicating a syntax problem.  Any advice?  

 

 

  • Quotes inside a quoted string must be doubled:

    .Range("AH11").Formula = "=COUNTIF($B11:$AF11, ""B"")"

  • JoeCavasin's avatar
    JoeCavasin
    Brass Contributor

    As always, thanks Hans.  Swear i tried that, but likely tried '"B"' instead, and regardless didn't come to the right solution.  

  • Quotes inside a quoted string must be doubled:

    .Range("AH11").Formula = "=COUNTIF($B11:$AF11, ""B"")"

Resources