• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

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

carts

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

example.gif

Any help is much appreciated! :)
 
Not VBA but a formula
A1: =IF(and(isodd(row(a1)),isodd(column(a1))),”Odd”, if(and(iseven(row(a1)),iseven(column(a1))),”Even”,””))
Copy across and down
 
Hi ,

Try this :
Code:
Public Sub FillOddsAndEvens()
           With Range("A1:T22")
                .Formula = "=IF(and(isodd(row(a1)),isodd(column(a1))),""Odd"", if(and(iseven(row(a1)),iseven(column(a1))),""Even"",""""))"
                .Value = .Value
           End With
End Sub
Narayan
 
Hi ,

Try this :
Code:
Public Sub FillOddsAndEvens()
          Dim fillrange As Range
          Dim filltext As String
          Dim numcols As Long, numrows As Long, fillcolor As Long, i As Long, j As Long

          Set fillrange = Range("A1:T22")
         
          With fillrange
                numcols = .Columns.Count
                numrows = .Rows.Count
          End With
           
          Application.ScreenUpdating = False
         
          For i = 1 To numrows
              If ((i Mod 2) = 0) Then
                  fillcolor = vbWhite
                  filltext = "Even"
              Else
                  fillcolor = vbYellow
                  filltext = "Odd"
              End If
             
              For j = 1 To numcols
                  With fillrange.Cells(i, j)
                        If ((j Mod 2) = (i Mod 2)) Then
                          .Value = filltext
                          .Interior.Color = fillcolor
                        End If
                        fillcolor = IIf(fillcolor = vbWhite, vbYellow, vbWhite)
                  End With
              Next
          Next
         
          Application.ScreenUpdating = True
End Sub
Narayan
 
I was looking at Narayan's code and thought I could improve it slightly

So I used a For each construct with a range object as shown below

Code:
Public Sub FillOddsAndEvens2()
Dim fillrange As Range
Dim filltext As String
Dim fillcolor As Long
Dim c As Range

Set fillrange = Range("A1:T22")

Application.ScreenUpdating = False

For Each c In fillrange
  If (c.Column Mod 2) = (c.Row Mod 2) Then
      fillcolor = vbYellow
      filltext = "Even"
      If ((c.Row Mod 2) = 1) Or ((c.Column Mod 2) = 1) Then filltext = "Odd"
  Else
      fillcolor = vbWhite
      filltext = ""
  End If

  c.Interior.Color = fillcolor
  c.Value = filltext

Next

Application.ScreenUpdating = True

End Sub

It works fine
upload_2018-5-6_11-35-32.png

However when I timed it it is actually about 40% slower than Narayan's
Results below are the average of 10 runs

FillOddsAndEvens
Time = 0.1097656 seconds

FillOddsAndEvens2
Time = 0.2628906 seconds

I am working on another technique and will post about it later
 
Technique 3

This time I save time by only writing to the Worksheet once

Code:
Public Sub FillOddsAndEvens3()
Dim fillrange As Range
Dim filltext As String
Dim numcols As Long, numrows As Long, fillcolor As Long, i As Long, j As Long
Dim myArr(1 To 22, 1 To 20) As String

Set fillrange = Range("A1:T22")

With fillrange
      numcols = .Columns.Count
      numrows = .Rows.Count
End With
Application.ScreenUpdating = False

For i = 1 To numrows
  If ((i Mod 2) = 0) Then
      fillcolor = vbWhite
      filltext = "Even"
  Else
      fillcolor = vbYellow
      filltext = "Odd"
  End If
 
  For j = 1 To numcols
      With fillrange.Cells(i, j)
        If ((j Mod 2) = (i Mod 2)) Then
            myArr(i, j) = filltext
            '.Interior.Color = fillcolor
        End If
      fillcolor = IIf(fillcolor = vbWhite, vbYellow, vbWhite)
      End With
  Next
Next

'Write all values to the worksheet
Range("A1:T22").Value = myArr

Application.ScreenUpdating = True
End Sub

I have cheated and used Conditional Formatting for the Coloring

FillOddsAndEvens
Time = 0.1097656

FillOddsAndEvens2
Time = 0.2628906

FillOddsAndEvens3
Time = 0.06210937

About 40% faster than FillOddsAndEvens
 
Back
Top