VBA Range Addressing in For Loop

  • Thread starter Stephen William Foyle
  • Start date
S

Stephen William Foyle

Hello, I am trying to write a macro that will add the values in a range of
cells with the values in the adjecent cells then clear those adjecent cells.
I have "hard coded" the macro by typing in each and every cell address, this
works, but I would like to use a For Loop:
---------------------------------
Sub OptionButton1_Click()
Dim z As Integer
For z = 8 To 22
Range("iz").Value2 = Range("iz").Value2 + Range("jz").Value2
Range("jz").Value2 = ""
Next z
For z = 26 To 40
Range("iz").Value2 = Range("iz").Value2 + Range("jz").Value2
Range("jz").Value2 = ""
Next z
For z = 46 To 51
Range("iz").Value2 = Range("iz").Value2 + Range("jz").Value2
Range("jz").Value2 = ""
Next z
End Sub
 
T

Tom Ogilvy

Sub OptionButton1_Click()
Dim z As Integer
For z = 8 To 22
Range("i" & z).Value2 = Range("i" & z).Value2 + Range("j" & z).Value2
Range("j" & z).Value2 = ""
Next z
For z = 26 To 40
Range("i" & z).Value2 = Range("i" & z).Value2 + Range("j" & z).Value2
Range("j" & z).Value2 = ""
Next z
For z = 46 To 51
Range("i" & z).Value2 = Range("i" & z).Value2 + Range("j" & z).Value2
Range("j" & z).Value2 = ""
Next z
End Sub
 
S

Stephen William Foyle

Thanks Tom It Works!
--
Stephen William Foyle


Tom Ogilvy said:
Sub OptionButton1_Click()
Dim z As Integer
For z = 8 To 22
Range("i" & z).Value2 = Range("i" & z).Value2 + Range("j" & z).Value2
Range("j" & z).Value2 = ""
Next z
For z = 26 To 40
Range("i" & z).Value2 = Range("i" & z).Value2 + Range("j" & z).Value2
Range("j" & z).Value2 = ""
Next z
For z = 46 To 51
Range("i" & z).Value2 = Range("i" & z).Value2 + Range("j" & z).Value2
Range("j" & z).Value2 = ""
Next z
End Sub
 

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

Top