VBA to add a formula that includes a question mark: Compile error

Tarver

Board Regular
Joined
Nov 15, 2012
Messages
109
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I'm trying to use VBA to replace some formulas in a spreadsheet when the user requests them. One of those should include question mark (?). I know this is a wildcard, but it works within my formula, not in VBA.

VBA Code:
Range("B14").Formula = "=IF($D14<>"","?","")"

I get the error "Compile Error: Expected: End of statement," and my question mark is highlighted.

If I precede question mark with a tilde, like so, I get a slightly different error: "Compile error: Invalid character."
VBA Code:
Range("B14").Formula = "=IF($D14<>"","~?","")"

How can I force VBA to think that the ? is a text character and not a wildcard?

Thanks in advance!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You need to double-up the quotes inside a string like
VBA Code:
Range("B14").Formula = "=IF($D14<>"""",""?"","""")"
 
Upvote 0
Ah, the ol' double-quotes! Thank you Fluff, as always!
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0
One more follow-up, Fluff. I am most definitely not a programmer. I had another issue come up today where I was trying to insert a formula and had another error. I was using quotes, like yesterday, and used this solution to know that I needed to double the quotation marks that are inside of my quotation marks in my VBA.

So, your answer is still paying dividends for me. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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