Embedded AND in IF statement not working

kc_native

Board Regular
Joined
Jan 20, 2009
Messages
176
Hi folks, the following is a formula I am entering into a cell using VBA, and is the resulting formula that is in the target cell after the code runs, not the code itself. I am getting a #NAME? error, and it apparently doesn't like the Cells portion of the formula. Can someone tell me why I can't use Cells(x,y) in this case? I need to keep this formula variable, in case the number of data columns changes in the worksheet.

=IF(AND(Cells(3,14)>0,Cells(3,15)<>""),"X","")
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Cells() is a VBA command, so Excel doesn't recognize it as a worksheet function.

Try

SomeCell.Formula = "=IF(AND(" & ActiveSheet.Cells(3,14). Address & ">0," & ActiveSheet.Cells(3,15).Address & "<>""""),""X"","""")"
 
Upvote 0
Cells() is a VBA command, so Excel doesn't recognize it as a worksheet function.

Try

SomeCell.Formula = "=IF(AND(" & ActiveSheet.Cells(3,14). Address & ">0," & ActiveSheet.Cells(3,15).Address & "<>""""),""X"","""")"
Thanks Jon! The only problem I see is that it creates a formula with the loacation locked ($Column$Row), but I want to take this formula and copy it down the column a variable number of rows. Is there a way to write the code to not make the cell position in the formula, absolute?
 
Upvote 0
.Address is the default, and provides absolute references.

.Address(RowAbsolute:=False, ColumnAbsolute:=False) overrides the default, providing relative references.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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