Loop within a loop?

ryancgarrett

Board Regular
Joined
Jun 18, 2011
Messages
122
For loop vba

Is it not possible to run a For loop within another For loop? In my code I want it to run a for loop for 3 to 250, but by the time it goes through one whole loop it has gone from 3 to 7, rather than 3 to 4. What am I doing wrong?

Code:
Sub postJournalEntries()
    Dim myRow As Integer
    Dim acctNum As Integer
    Dim otherRow As Integer
    
    With Sheets("Journal")
        For myRow = 3 To 250
            If Cells(myRow, 1).Value <> Empty And Cells(myRow, 4) <> "Posted" Then
                acctNum = BlankRow("Journal", myRow, 2) - 2
                otherRow = myRow

                    For otherRow = otherRow To acctNum
                        If Cells(otherRow, 5).Value > 0 Then
                            Sheets(Cells(otherRow, 2).Value).Cells(BlankRow(Cells(otherRow, 2).Value, 3, 1), 1).Value = Date
                            Sheets(Cells(otherRow, 2).Value).Cells(BlankRow(Cells(otherRow, 2).Value, 3, 1) - 1, 4).Value = Cells(otherRow, 5).Value
                        ElseIf Cells(otherRow, 7).Value > 0 Then
                            Sheets(Cells(otherRow, 2).Value).Cells(BlankRow(Cells(otherRow, 2).Value, 3, 1), 1).Value = Date
                            Sheets(Cells(otherRow, 2).Value).Cells(BlankRow(Cells(otherRow, 2).Value, 3, 1) - 1, 5).Value = Cells(otherRow, 5).Value
                        End If
                            Cells(otherRow, 4).Value = "Posted"
                    Next
            End If
        Next
    End With
End Sub

'Function to find the end of a list
Function BlankRow(sName As String, sRow As Integer, sCol As Integer) As Integer
    Do Until Sheets(sName).Cells(sRow, sCol).Value = Empty
        sRow = sRow + 1
    Loop
    
    BlankRow = sRow
End Function

Note, I have noticed that it adds to the value of myRow as it runs through the BlankRow function.
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Ryan,

You are passing your loop counter (myRow) as a reference, and it gets increased in the BlankRow function

acctNum = BlankRow("Journal", myRow, 2) - 2

Try changing your function from:

Function BlankRow(sName As String, sRow As Integer, sCol As Integer) As Integer

To
Function BlankRow(sName As String, ByVal sRow As Integer, sCol As Integer) As Integer
 
Upvote 0
I'm not sure why you're helping people with VBA at 2 AM but I'm pretty sure you are an angel! Lol thanks so much!
 
Upvote 0
You can fully avoid BlankRow function by using only one line:
Code:
...
acctNum = Worksheets("Journal").Cells(myRow, 2).End(xlDown).Row -[B][COLOR="Red"] 1[/COLOR][/B]
...
 
Last edited:
Upvote 0
Haha I don't think I've ever been called an angel, but it has a nice ring to it. Thanks :)

Onto my 6th cup of coffee...
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,550
Members
452,927
Latest member
rows and columns

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