IF statement inside formula

AyuHI

New Member
Joined
Dec 9, 2015
Messages
4
Hi guys, I hope you can help me with a code I'm trying unsuccessfully to make work.

At a certain point in my code, it is necessary to write a formula in a cell that presents an IF statement of the type:
=IF(JANUARY!$E2=TRUE;"Correct";"")

So basically it checks if Cell E2 in sheet JANUARY equals TRUE. If it does, it writes "Correct" and if false it writes nothing. Without VBA (so simply typing it) it is working fine.

Now I want the month in that formula to be a variable, as well as the Row number (the column will always be the same).
I created a variable called MonthName and another one called UnitRow, they are both working (MonthName currently is FEBRUARY and UnitRow = 4) but the formula as I wrote returns an error in VBA.

Here's how it is now:

ActiveCell.Formula = "=IIF(" & MonthName & "!$E" & UnitRow & "=TRUE;""Correct"";"""")"


Could someone help me with this?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
A few problems I can see.

1. I don't think you can use MonthName as a variable in VBA because it's an existing VBA function.
Try changing that to mnth or something else.

2. There's no such function as IIF (that's probably just a typo)
3. VBA uses commas not semicolons, regardless of your regional settings.

Try

Mnth = "January"
ActiveCell.Formula = "=IF(" & Mnth & "!$E" & UnitRow & "=TRUE,""Correct"","""")"
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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