Referencing Ranges in IF THEN Statement in VBA

klugdav

New Member
Joined
Feb 8, 2012
Messages
11
Hi,

I am trying to write an IF THEN statement that checks to see if a cell is less than certain cells on a different sheet. I got the macro to work with hard inputs, but I can't seem to figure out how to input the ranges I would like to input. Below is my formula:

ActiveCell.FormulaR1C1 = "=IF(RC[-1] < ?, 1, IF(RC[-1] < ?, 2, IF(RC[-1] < ?, 3, IF(RC[-1] < ?, 4, IF(RC[-1] < ?, 5,0)))))"

For each ? I want to put in a reference to a cell in a named range. The references are as follows:

Range("PREINPUTS_CHILLERINFO").Cells(1,3)
Range("PREINPUTS_CHILLERINFO").Cells(2,3)
Range("PREINPUTS_CHILLERINFO").Cells(3,3)
Range("PREINPUTS_CHILLERINFO").Cells(4,3)
Range("PREINPUTS_CHILLERINFO").Cells(5,3)

Each time I try to put in the reference it won't run. In the end I want to copy and paste this cell down into a whole range. Would it be easier to use the range formulas instead?

Thanks for the help.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I'll do the first one, you should be able to infer how to do the rest..

ActiveCell.FormulaR1C1 = "=IF(RC[-1] < ?, 1, ....
would be
ActiveCell.FormulaR1C1 = "=IF(RC[-1] <" & Range("PREINPUTS_CHILLERINFO").Cells(1,3) & ", 1, ....


Or

Var1 = Range("PREINPUTS_CHILLERINFO").Cells(1,3)

ActiveCell.FormulaR1C1 = "=IF(RC[-1] < " & Var1 & ", 1, ....
 
Last edited:
Upvote 0
Maybe:

Code:
ActiveCell.FormulaR1C1 = "=IF(RC[-1]<'PREINPUTS_CHILLERINFO'!R1C3,1,IF(RC[-1]<'PREINPUTS_CHILLERINFO'!R2C3,2,IF(RC[-1]<'PREINPUTS_CHILLERINFO'!R3C3,3,IF(RC[-1]<'PREINPUTS_CHILLERINFO'!R4C3,4,IF(RC[-1]<'PREINPUTS_CHILLERINFO'!R5C3,5,0)))))"
 
Upvote 0
Hi,

I am trying to write an IF THEN statement that checks to see if a cell is less than certain cells on a different sheet. I got the macro to work with hard inputs, but I can't seem to figure out how to input the ranges I would like to input. Below is my formula:

ActiveCell.FormulaR1C1 = "=IF(RC[-1] < ?, 1, IF(RC[-1] < ?, 2, IF(RC[-1] < ?, 3, IF(RC[-1] < ?, 4, IF(RC[-1] < ?, 5,0)))))"

For each ? I want to put in a reference to a cell in a named range. The references are as follows:

Range("PREINPUTS_CHILLERINFO").Cells(1,3)
Range("PREINPUTS_CHILLERINFO").Cells(2,3)
Range("PREINPUTS_CHILLERINFO").Cells(3,3)
Range("PREINPUTS_CHILLERINFO").Cells(4,3)
Range("PREINPUTS_CHILLERINFO").Cells(5,3)

Each time I try to put in the reference it won't run. In the end I want to copy and paste this cell down into a whole range. Would it be easier to use the range formulas instead?

Thanks for the help.


Try:

ActiveCell.FormulaR1C1 = "=IF(RC[-1]<" &
Range("PREINPUTS_CHILLERINFO").Cells(1,3) &",1,IF(RC[-1]<" &
Range("PREINPUTS_CHILLERINFO").Cells(2,3) &",2,IF(RC[-1]<" &
Range("PREINPUTS_CHILLERINFO").Cells(3,3) &",3,IF(RC[-1]<" &
Range("PREINPUTS_CHILLERINFO").Cells(4,3) &",4,IF(RC[-1]<" &
Range("PREINPUTS_CHILLERINFO").Cells(5,3) & ", 5,0)))))"
 
Upvote 0
Thanks jonmo1, I used the second method and it worked. Part of my problem was that it was referencing a table that isn't necessarily filled out all the time. Once I entered in zeros for the missing data it worked fine.
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,914
Members
449,132
Latest member
Rosie14

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