Simple VBA issue inserting formula

easybpw

Active Member
Joined
Sep 30, 2003
Messages
437
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi everyone. Thanks in advance for the help. I'm trying to teach myself some simple VBA commands and have a question. I'm sure its easy fo all of you with even a slight bit of knowledge. I want to insert a formula in a cell if another cell contains Y. If it doesn't contain a Y then I want the cell to be blank. Below is what I have. I can do it if I jusst want to add the cells together but cannot get it to work if I have a condition in another cell.

Sub formula_Example()
Range("B3").Formula = "=if(a1,"Y",b1+b2," ")"
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You need to double-up on the quotes inside the formula like.
VBA Code:
Sub formula_Example()
Range("B3").Formula = "=if(a1=""Y"",b1+b2,"""")"
End Sub
 
Upvote 0
Solution
You need to double-up on the quotes inside the formula like.
VBA Code:
Sub formula_Example()
Range("B3").Formula = "=if(a1=""Y"",b1+b2,"""")"
End Sub
Thanks for the quick response. I added the double quotes and it seemed to run the first time, or at least the results were correct on the worksheet but I got a VBA 400 error and now when I run the macro I just get the error. Doing some research on the error it basically is saying the macro crashed. One of the steps to correct this is to turn on the trusted access, which I did but still get the error. If this is more involved than a simple mistake on my part then I'll do more research.

Sub formula_Example()
Range("B3").Formula = "=if(a1,""Y"",b1+b2,"""")"
End Sub
 
Upvote 0
That is not the code I suggested.
 
Upvote 0
That is not the code I suggested.
I just now copied and pasted the code you provided and it worked, obviously. Sorry but I am not seeing the difference between how I updated it and what you provided although there must be one because it now works. Just can't see where I am different. But I appreciate this board and the help you gave me.
 
Upvote 0
You have a comma after the cell reference, instead of an = sign
 
Upvote 0
@Fluff be like HAWKEYE, took me a while to spot even after I read the post above.
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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