VBA syntax error

Coryj5

Board Regular
Joined
Sep 27, 2011
Messages
76
I am trying to make some corrections to a series of identicle tabs in a sheet, so i created this macro.

Sub corrections()

With Range ("M15")
.Value = "=COUNTIF(A15:A21,"YES")"
End with

End Sub
.... along with some other corrections, but the root lies in this one action.

I receive a syntax error being caused by the quotes around YES, but that is required for the formula to work properly.
Am i doing something silly that is right in front of me here?...

Thanks,

coryj5
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
When entering a formula in a cell via VBA, and that formula contains quotes...
You have to double up the quotes.

Also, try using the .Formula property, isntead of .Value

Rich (BB code):
With Range ("M15")
    .Formula = "=COUNTIF(A15:A21,""YES"")"
End with
 
Upvote 0
Try...

Code:
With Range ("M15")
     .Formula = "=COUNTIF(A15:A21,""YES"")"
End with
 
Upvote 0
Text constants are surrounded by quote marks... quote marks within them must be doubled-up (the method VB uses to see a quote mark as a quote mark character without any special meaning) so VB can tell those internal quotes are not marking the beginning or end of a text constant (if they did, they are missing the ampersand concatenation symbols which is why you are getting the syntax error). Try your line like this...

Code:
.Value = "=COUNTIF(A15:A21,""YES"")"
 
Upvote 0
Worked perfect! thanks all.

I did try and use the .formula for an if statement and that did not work. i had to use .value.
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,317
Members
452,905
Latest member
deadwings

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top