VBA Insert Formula with Variable into Cell

The Dutchman

Board Regular
Joined
Apr 10, 2008
Messages
72
I had a sumif formula in a range of cells to caluculate the total transactions for a series of respective dates. Because it is an ongoing database I had the sumif range going from rows 7 to 65536. This really affected the speed of my template.

What I am trying to do is replace the static formula range mentioned above and replace it with a dynanic range based on the cells that are actually occupied using code. The user will move to this part of the template by using a commandbutton.

I want to replace the first cell(which currently has a static sumif statement) at the top of the form with a dynamic one. After I have inserted the formula via code I want to copy that formula down the cells below it.

I believe the problem is trying to pass the variable "nextrow" in the formula because it has " " marks around it.


The code I have developed is as follows:
Code:
nextrow = Range("FA65536").End(xlUp).Row + 1

Worksheets(1).Range("EE7").Formula = _
    "=SumIf(Range("$FA$7:$FA$" & nextrow), Range("$EA7"), _    
          Range("$FA$7:$FH$" & nextrow))"

Worksheet(1).Range("EE7").Copy Range("EE8:EE37")

Any ideas?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Make a new string first, then pass that string to the Formula
Code:
TempString = "=SumIf(Range(""$FA$7:$FA$" &" & nextrow & "), Range(""$EA7""), _    
          Range(""$FA$7:$FH$" &" & nextrow & "))"
 
Worksheets(1).Range("EE7").Formula = TempString
 
Upvote 0
Thx Stephan:

I had to the cell reference quotation marks and all the references of "Range"... but it worked like a charm!

Ray
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,814
Members
448,990
Latest member
rohitsomani

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