Use of Variable within Formula

Sean Stevens

Board Regular
Joined
Jul 24, 2003
Messages
123
I am getting a run-time 1004 error with the following formula.

I am using the variable 'maxrow' which is defined as an integer, but there must be a problem with the way I have created the formula.


Sheets("Finance").Range("K" & maxrow).Formula = "=SUMIF($I$5:$I$ & maxrow,""Internal"",K5:K & maxrow)"

Can anyone spot the problem??
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
More quotes needed in the formula itself. Try:

Sheets("Finance").Range("K" & maxrow).Formula = "=SUMIF($I$5:$I$" & maxrow & ",""Internal"",K5:K" & maxrow & ")"

It's a string so all the known parts should be surrounded by quotes and the variable not.
 
Upvote 0
REplace Formula with value

Like

Sheets("Finance").Range("K" & maxrow).Value = "=SUMIF($I$5:$I$ & maxrow,""Internal"",K5:K & maxrow)"

Also dont quote the maxrow or any vaiable that you want the value from

Lets say MaxRow = 5 if you put ="MaxRow" then it will equal the work maxrow if you put = maxrow it will equal 5

i.e. when you get to the part "=SUMIF($I$5:$I$ & ... after the last $ you need to close the quotes.

HTH

Jacob
 
Upvote 0
Cheers guys - both work.

This will help me out loads, as I want to code a lot of VBA formulas that make use of variables.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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