Macro for summation with a variable

lovi

New Member
Joined
Jul 22, 2010
Messages
13
Hello all, the algorithm below is meant to open every xls file in a folder then calculate a sum but each workbook have a different number of rows. Im trying to write the sum command with a variable 'ru' to accomodate for this but no success so far. Here's the macro I have, can someone have a quick look and point out my error please?

Any help is really appreciated

Lovi


Code:
Option Explicit
Sub Open_All_Files()
Dim oWbk As Workbook
Dim sFil As String
Dim sPath As String
Dim lr As Long
Dim ru As Integer
sPath = "C:\New folder"
ChDir sPath
sFil = Dir("*.xls")
Do While sFil <> ""
    Set oWbk = Workbooks.Open(sPath & "\" & sFil)
    lr = Cells(Rows.Count, "H").End(xlUp).Row
    ru = lr - 5
    ActiveSheet.Range("N5").Value = ru
    ActiveSheet.Range("N6") = "=SUM(R[5]C[-2]:R[ru]C[-2])"
    
    oWbk.Close True
sFil = Dir
Loop
End Sub
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Lovi,
your main problem is:
ActiveSheet.Range("N6") = "=SUM(R[5]C[-2]:R[ru]C[-2])"
I guess that has to be:
ActiveSheet.Range("N6").formulaR1C1 = "=SUM(R[5]C[-2]:R[ru]C[-2])"

Secondly, that formula means:
R[5]C[-2] = L11 -> 5 cells down and 2 columns left from the cell where you put the formula.
Say, the last found row (lr) is 100, then ru = 95
R[ru]C[-2] = L101

I guess with that logic you should be able to repair that line (as I don't know what you want to add up in cell N6).

Cheers,
Koen
 
Upvote 0
Since you are using a variable in your formula string, you would need to structure it like this
Rich (BB code):
"=SUM(R[5]C[-2]:R[" & ru & "]C[-2])"

If that does not solve your problem, please try to explain in words, just what you are trying to sum on each sheet.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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