0
votes

I have a series of Excel files all in the same format. The amount of data in each excel file differs. I am making a code that will loop through a folder and perform calculations with all excel files.

I am using the following code in VBA to determine the number of rows in an excel file:

 i = .Cells(.Rows.Count, 2).End(xlUp).Row

I am using the following formula to perform calculations on a range of cells:

With .Range("D2:D" & i)
     .Formula = "=Log(B2)"
End With

How would I calculate the sum of all values in the "D" column in the next available cell in D? This is how the formula would look in theory

j = i + 1
    With .Range("D" & j)
         .Formula = "=Sum(D2:D & i)"
    End With

How would I use the D cell with the sum for future calculations? Lets say I wanted E1 "=(D2-$D$ & j)^2"

Sorry for the vague title, I didn't know how to describe this problem.

1
should it be .Formula = "=SUM(D2:D" & i & ")"?Dmitry Pavliv
Yes, that's what I was looking for!JC11

1 Answers

2
votes

As follow up from comments, this one works:

With .Range("D" & j)
     .Formula = "=SUM(D2:D" & i & ")"
End With