Sum macro for a variable "i" in a loop

  • Thread starter Thread starter ucanalways
  • Start date Start date
U

ucanalways

I am trying to do the following sum macro but I get an error for the
sum fomula. I want to sum the values from range B2 to B&i, the
corresponding value of "i".. Please let me know how do I do this.
Thanks

Sub test()
Dim i As Integer
For i = 1 To 100
Range("A1").Value = Sum(Range(B2, B&i))
Next i
End Sub
 
Sub test()
Dim i As Long
For i = 1 To 100
Range("A1").Value = application.Sum(Range("b2:B" & i))
Next i
End Sub
 
One way:

Public Sub test()
Dim i As Integer
For i = 1 To 100
Range("A1").Value = _
Application.WorksheetFunction.Sum("B2:B" & i)
Next i
End Sub
 
Range("A1").Value = Application.Sum(Range("B2:B & i))

is all you need, no loop

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
After reading Bob's post, I'm not sure what you wanted.

Range("A1").Value = application.Sum(Range("B2:B100"))

or maybe something like:

Sub test()
Dim i As Long
For i = 1 To 100
Range("A" & i).Value = application.Sum(Range("b2:B" & i))
Next i
End Sub
 
But which "i" should be used? <g>

My impression was that this was just a test...
 
I'll leave that to the OP's discretion <g^2>

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top