vba loop with multiple variables (nested?) or one variable with different step increments

ExcelArtist

New Member
Joined
Jun 1, 2014
Messages
3
Hello all,

I've created a macro where I add values from one column with 2 values in another column that looks like this:

Range("a1").Select
ActiveCell.Value = "=sum(d1,f1:f2)"

Range("a2").Select
ActiveCell.Value = "=sum(d2,f3:f4)"


Range("a3").Select
ActiveCell.Value = "=sum(d3,f5:f6)"

This code also does what I want, but without using a loop(s):

Cells(1,1).Value = Cells(1,1).Offset(0,3).Value + Cells(1,1).Offset(0,5).Value + Cells(1,1).Offset(1,5).Value
Cells(2,1).Value = Cells(1,1).Offset(1,3).Value + Cells(1,1).Offset(2,5).Value + Cells(1,1).Offset(3,5).Value
Cells(3,1).Value = Cells(1,1).Offset(2,3).Value + Cells(1,1).Offset(4,5).Value + Cells(1,1).Offset(5,5).Value


I've tried using nested for next loops and a nested do until loop, but it seems like this isn't the right solution.
The closest I've gotten is to increment the variable by step 2, but the results are then placed in every other row

Any help / suggestions on how to use multiple variables in a nested loop would be greatly appreciated! Thanks!!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this

VBA Code:
Sub loop_multiple()
  Dim i As Long, n As Long
  n = 1
  For i = 1 To 3
    Cells(i, "A") = Cells(i, "D") + Cells(n, "F") + Cells(n + 1, "F")
    n = n + 2
  Next
End Sub
 
Upvote 0
Try this...
VBA Code:
Dim x As Range
Dim y As Long
    y = 1

For Each x In Range("A1:A3")
    x.Value = "=SUM(D" & x.Row & ",F" & y & ":F" & y + 1 & ")"
    
    y = y + 2
Next x
 
Upvote 0
Thank you both so much! It was simple and even though I was close, it would have taken me a long time figure this out. Appreciate the new info!!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,470
Messages
6,124,992
Members
449,201
Latest member
Lunzwe73

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