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 3
An Average Forum Participant Just For FunHardware Tester and DebuggerAsked:
Who is Participating?
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
0
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

0

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 :-(
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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 :)
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
In that case, there is so much to learn for you in the code I posted.
0
An Average Forum Participant Just For FunHardware Tester and DebuggerAuthor Commented:
I guess it's alright! then. Thank you for all of your help! :)
0
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 :)
0
AlanConsultantCommented:
In that case, I'd say the code above is just about exactly what you want.

Alan.
1
Bill PrewCommented:
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
0
hnasrCommented:
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

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.