vba. looping in a triangle

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
986
Office Version
  1. 2010
Platform
  1. Windows
Hello People.

VBA Code:
Sub pr_1()
Dim Row As Integer
Dim Column As Integer
      For Row = 2 To 7
            For Column = 2 To 7
                  Cells(Row, 9).Value = Cells(Row, 9).Value + Cells(Row, Column).Value
            Next
      Next
End Sub
With this code I got this:
1618335666977.png

Adding row by row and results on "I".
I am looking for adding just the triangle, here just for illustration and simplicity is the number 5 but could be any value, so the expecting results would be
1618336067236.png

so. what I have to change in order to get it.
I appreciate you reading this,
would be nice if someone give me a hand here.
Please.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about
VBA Code:
   With Range("I2:i7")
      .Formula = "=sumif(b2:g2,max(b2:g2))"
      .Value = .Value
   End With
 
Upvote 0
Thanks Fluff, something is not working with this formula inside with---end with.
Sir, with all respect, any possibility to tell me how, just to modify the line:

VBA Code:
Cells(Row, 9).Value = Cells(Row, 9).Value + Cells(Row, Column).Value
Please.
my concern is the logic behind, I am experimenting with the for loop
I would like to learn how manipulate this line in order to get the results.
The statement With____end with, still is a confusing concept or idea, because if you can Repeat anything inside, then any loop becoming useless, and of course, I am not sure about this either.
"Thank you "
 
Upvote 0
You will need to use an If statement to see if the cell is equal to the value you want & if so add it.
 
Upvote 0
Fluff thank you for your tutorial, I did it.
VBA Code:
Sub pr_1()
Dim Row As Integer
Dim Column As Integer
      For Row = 2 To 7
            For Column = 2 To 7
                  If Row = Row + colum Then
                          Cells(Row, 9).Value = Cells(Row, 9).Value + Cells(Row, Column).Value
                  End If
            Next Column
      Next Row      
End Sub
 
Upvote 0
That is doing exactly the same as your original code. :confused:
 
Upvote 0
Try this code:
VBA Code:
Option Explicit

Sub pr_1()
Dim Row As Integer
Dim Column As Integer
Dim ItemCounter As Integer, ColumnCount As Integer
Dim StartRow As Integer, StartColumn As Integer, RowCount As Integer
StartRow = 11
StartColumn = 2
RowCount = 6
ItemCounter = 0
  For Row = StartRow To StartRow + RowCount - 1
    ItemCounter = ItemCounter + 1
    For Column = StartColumn To StartColumn + ItemCounter - 1
      Cells(Row, 9).Value = Cells(Row, 9).Value + Cells(Row, Column).Value
    Next
  Next
End Sub
 
Upvote 0
Amended your original code:
VBA Code:
Sub pr_1()
    Dim Row As Integer
    Dim Column As Integer, c As Integer
    For Row = 2 To 7
        c = c + 1
        For Column = 2 To 1 + c
            Cells(Row, 9).Value = Cells(Row, 9).Value + Cells(Row, Column).Value
        Next
    Next
End Sub
 
Upvote 0
Solution
vw412 and aRandomHelper, I like both and I click as solution, thank you both are a good lesson, I am answering only now because I work second shift so, I just turn on my pc, and yoohoo, nice people like always in this forum, "Great lesson", thank you thank you.
Now I will start to enjoy a new pattern and check how much I am able to accomplish. I love VBA. specially loops.
 
Upvote 0
It looks like the goal is either the sequence 5, 5+5, 5+5+5 , ....5*n
or that a loop like
VBA Code:
For Row = 1 to 6
   For Column = 1 to i
        Sum = Sum + Cells(Row+1, Column+1).Value
   Next
Next
 
Upvote 0

Forum statistics

Threads
1,224,752
Messages
6,180,742
Members
452,996
Latest member
nelsonsix66

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