Results 1 to 1 of 1

Recursively traverse through a range and assign a value to each cell in range

Threaded View

  1. #1
    Registered User
    Join Date
    11-11-2010
    Location
    Atlanta
    MS-Off Ver
    Excel 2010
    Posts
    1

    Recursively traverse through a range and assign a value to each cell in range

    To learn recursive programming and trying to write a simple VBA to traverse to every cell in a defined range and put 1 into the cells. (I know this can be done easily through iteration by using two loops.)

    The following script fills up to 3346 cells before it threw Error 28 "Out of Stack spaces".

    Is it an error on my script or Excel's limit on stack? Thank you for your help.

    Sub test()
        Dim L As String
        L = rWrite(Range("A1:Z300"), 1, 1, 1, Range("A1:Z300").Rows.Count, Range("A1:Z300").Columns.Count)
    End Sub
    
    Function rWrite(ByRef rArray As Range, iVal As Integer, CurRow As Integer, CurCol As Integer, RowsCount As Integer, ColsCount As Integer) As String
    
        If CurCol <= ColsCount Then
            If CurRow <= RowsCount Then
                rArray.Cells(CurRow, CurCol) = iVal
                rWrite = rWrite(rArray, iVal, CurRow + 1, CurCol, RowsCount, ColsCount)
            Else
                rWrite = rWrite(rArray, iVal, 1, CurCol + 1, RowsCount, ColsCount)
            End If
        Else
            rWrite = "Done"
        End If
    End Function
    --
    Chris Win
    Last edited by itzchau; 11-11-2010 at 12:57 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1