Needs Help with a Nested "FOR.. NEXT" Loop

Hi everyone! I am trying to write a nested “FOR… NEXT” loop to create the following table in Excel

Example
Any help is much appreciated! :)
LVL 4
An Average Forum Participant Just For FunHardware Tester and DebuggerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlanConsultantCommented:
Not sure what the reading night be, but it would be trivial with a formula.

Why VBA?


Alan
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
What about this? The code will prompt you to select a range on the sheet and then populate it with Odd or Even.
Sub PopulateRangeWithOddEven()
Dim rng As Range, cell As Range
On Error Resume Next
Set rng = Application.InputBox("Select the range to be populated.", "Select Range!", Type:=8)
On Error GoTo 0

If rng Is Nothing Then
    MsgBox "You didn't select any range.", vbExclamation
    Exit Sub
End If

Application.ScreenUpdating = False
rng.ClearContents

For Each cell In rng
    If WorksheetFunction.IsOdd(cell.Row) And WorksheetFunction.IsOdd(cell.Column) Then
        cell.Value = "Odd"
    ElseIf WorksheetFunction.IsEven(cell.Row) And WorksheetFunction.IsEven(cell.Column) Then
        cell.Value = "Even"
    End If
Next cell
Application.ScreenUpdating = True
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlanConsultantCommented:
*reason might*

Editing stopped working on mobile for me a while ago :-(
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

An Average Forum Participant Just For FunHardware Tester and DebuggerAuthor Commented:
Hi Alan! I am actually learning how to use vba and I posted this so that I can get an example as reference :)
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
In that case, there is so much to learn for you in the code I posted.
An Average Forum Participant Just For FunHardware Tester and DebuggerAuthor Commented:
I guess it's alright! then. Thank you for all of your help! :)
An Average Forum Participant Just For FunHardware Tester and DebuggerAuthor Commented:
Hi all! I will be leaving this question open for a while longer if anyone has any alternative codes :)
AlanConsultantCommented:
In that case, I'd say the code above is just about exactly what you want.

Alan.
Bill PrewIT / Software Engineering ConsultantCommented:
And since you asked about nested loops, here's an approach that shows that, iterating over each row in the range, and within each row looks at each column one by one.

Sub PopulateRangeWithOddEven()
    Dim rng As Range
    Dim r As Long
    Dim c As Long
    
    Set rng = Range("A1:G10")

    rng.ClearContents

    For r = 1 To rng.Rows.Count
        For c = 1 To rng.Columns.Count
            Select Case (r And 1) + (c And 1)
                Case 2
                    rng.Cells(r, c) = "Odd"
                Case 0
                    rng.Cells(r, c) = "Even"
            End Select
        Next
    Next

End Sub

Open in new window


»bp
Hamed NasrRetired IT ProfessionalCommented:
I am trying to write a nested “FOR… NEXT” loop
Try this:
Sub Fill_Odd_Even()
    Dim r As Range
    Dim ri As Long
    Dim ci As Long
    Dim rmax As Long
    Dim cmax As Long
    
    Set r = Range("A1:W23")
    rmax = r.Rows.Count
    cmax = r.Columns.Count
    For ri = 1 To rmax Step 2
        For ci = 1 To cmax Step 2
            Cells(ri, ci).Value = "Odd"
            If ri + 1 > rmax Or ci + 1 > cmax Then
            Else
                Cells(ri + 1, ci + 1).Value = "Even"
            End If
        Next ci
    Next ri
    
End Sub

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.