Formula to code

fairwinds

MrExcel MVP
Joined
May 15, 2003
Messages
8,638
Hi,
Could anyone help me with the code that does the same as this formula =SUMIF($A3:INDIRECT(ADDRESS(ROW()+$CO2;1;4));$A2+1;CX3:INDIRECT(ADDRESS(ROW()+$CO2;COLUMN();4)))*$D2+J2*$D2

I have a macro that puts this in several cells and then copy and paste special values.

Cells(a, 102).FormulaR1C1 = _
"=SUMIF(R[1]C1:INDIRECT(ADDRESS(ROW()+RC93,1,4)),RC1+1,R[1]C:INDIRECT(ADDRESS(ROW()+RC93,COLUMN(),4)))*RC4+RC[-92]*RC4"

However I cannot figure out the code that just does the job.

Thanks in advance
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I try to clarify and see if I get some response.
This is a part of a multilevel price calculation.
Level 1 is highest level.
Product level 1 consists of several level 2’s.
Each level two of several level 3’s and so on.
Up to ten levels and unlimited number of products.
The formula is on every row and reaches down until it finds the same level or higher. (That is given by the $CO2 reference). And then summarises only the levels that is one level lower within that area. I’ve tried to solve this with for next loops that checks every row but it takes forever as it is several thousand rows. I would like to apply the mentioned formula to each cell going from bottom up without actually pasting it into the cell.
Any help is highly appreciated!
 
Upvote 0
Well you could just do this:

Code:
Cells(a, 102).FormulaR1C1 = _ 
"=SUMIF(R[1]C1:INDIRECT(ADDRESS(ROW()+RC93,1,4)),RC1+1,R[1]C:INDIRECT(ADDRESS(ROW()+RC93,COLUMN(),4)))*RC4+RC[-92]*RC4"
Cells(a, 102).Value = Cells(a, 102).Value
 
Upvote 0
Thanks! it seems to be speeding it up a little. Which one do yu think is fastest. Either turn autmatic calc of, paste code in several cells, calc on and then do our trick or to do it cell by cell? (this formula and others go like 30 times per row)
 
Upvote 0
You can use the FormulaRIC1 property with a Range to populate all the cells at once. Here is a simple example:

Code:
Sub Test()
    Dim a As Long
    a = 1
    With Range(Cells(a, 2), Cells(a + 4, 2))
        .FormulaR1C1 = "=RC[-1]"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Yes! That was it. Your solution uses like 10% of the time as mine. (Strange though as it still calculates every cell one by one)

Many thanks!
 
Upvote 0
A formula with INDIRECT/ADDRESS (both are volatile), copied to hundreths of cells is bound to take ages to compute.
 
Upvote 0
Yes but as it replaces several levels of loops it is actually not that big difference. What Andrew did for me was to ensure that they did not recalk more than once. That did it for me.
 
Upvote 0

Forum statistics

Threads
1,221,709
Messages
6,161,431
Members
451,705
Latest member
Priti_190

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