Excel Vba how to insert formula with code

Javi

Active Member
Joined
May 26, 2011
Messages
440
I have a range of cells I would like to insert the below formula into. The range is “G5:G10000” keeping in mind the formula needs to change for each row as it would if you copied and pasted it down a column.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
=IF(AND(E5>=$C$1,E5<=$D$1-1),$C$2,IF(AND(E5>=$D$1,E5<=$E$1-1),$D$2,IF(AND(E5>=$E$1,E5<=$F$1-1),$E$2,IF(AND(E5>=$F$1,E5<=$G$1-1),$F$2,IF(AND(E5>=$G$1,E5<=$H$1-1),$G$2,IF(AND(E5>=$H$1,E5<=$I$1-1),$H$2,IF(AND(E5>=$I$1,E5<=$J$1-1),$I$2,IF(AND(E5>=$J$1,E5<=$K$1-1),$J$2,0))))))))

Thanks for the assistance
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try

Code:
Range("G5:G10000").Formula = "=IF(AND(E5>=$C$1,E5<=$D$1-1),$C$2,IF(AND(E5>=$D$1,E5<=$E$1-1),$D$2,IF(AND(E5>=$E$1,E5<=$F$1-1),$E$2,IF(AND(E5>=$F$1,E5<=$G$1-1),$F$2,IF(AND(E5>=$G$1,E5<=$H$1-1),$G$2,IF(AND(E5>=$H$1,E5<=$I$1-1),$H$2,IF(AND(E5>=$I$1,E5<=$J$1-1),$I$2,IF(AND(E5>=$J$1,E5<=$K$1-1),$J$2,0))))))))"
 
Upvote 0
one way to do this without code is select cell G5, then in the name range box where it should say G5 click the box and type G5:G10000 and press enter, this will now select the range, now type in the formula but dont just press enter, press CTRL + ENTER, this will put the formula in all the cells selected

if you already have the formula in G5 after typing the range in the named range box press F2 to edit the formula the CTRL ENTER to insert the formula to the rest

HTH
 
Upvote 0
Thanks All!!

I used this code and it worked well.

Dim my_range As Range

With Worksheets("Analyzer - Calculator")

Set my_range = .Range("G5:G10000")
my_range.Formula = _
"MyFormulawenthere"

How do you put the code in a separate window on this forum? YES I'm new to this!


End With
 
Upvote 0
Hi Javi,

In case it's any use to you, I think you can shorten your formula to achieve the same result - try the following array formula (entered with CTRL-SHIFT-ENTER):

=IF(SUM((E5>=$C$1:$J$1)*(E5<=$D$1:$K$1-1))=0,0,INDEX(($C$2:$J$2),MATCH(1,(E5>=$C$1:$J$1)*(E5<=$D$1:$K$1-1),0)))

The first part checks for no match in which case a 0 is returned. The next part finds and returns the first category where both conditions are true.

or if you have Excel 2007+ then you can simplify the error checking with the following (also entered with CTRL-SHIFT-ENTER):

=IFERROR(INDEX(($C$2:$J$2),MATCH(1,(E5>=$C$1:$J$1)*(E5<=$D$1:$K$1-1),0)),0)
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,681
Members
452,937
Latest member
Bhg1984

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