Consulting

Results 1 to 5 of 5

Thread: Strange behavior of for... next loop

  1. #1

    Strange behavior of for... next loop

    I am using a nested loop that finds a 150*150 matrix which determines the row and column number of each cell [e.g. cell D4 is (4, 4)]. This works fine but when cells reach columns greater than 99, strange results appears; the row number becomes minus, and the parentheses disappear. This is shown in the screenshot below.
    I am using Excel 2013, and here is the code in the vbe

    Option Explicit
    Sub m()
    Dim i, j As Integer
    For i = 1 To 150
    For j = 1 To 150
    Cells(i, j) = "(" & i & "," & j & ")"
    Next j
    Next i
    End Sub
    Regards,

    Hani

    Clip_161.jpgClip_162.jpg

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    As written, 2010 does the same

    When you try to enter I = 1 and J = 100 Excel sees "(1,100)" as negative 1,100

    Something like this works

    Option Explicit
    Sub m()
        Dim i As Long, j As Long ' BTW, the way you had it the I was dimmed as a variant, since you didn't explicitly say 'As Long'
        For i = 1 To 150
            For j = 1 To 150
                ActiveSheet.Cells(i, j).NumberFormat = "@"
                ActiveSheet.Cells(i, j).Value = "(" & i & "," & j & ")"
            Next j
        Next I
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    I agree with Paul. This is my try. I also added a little something to make it slightly faster to perform.

    Sub m()    
        Dim i As Integer
        Dim j As Integer
        Dim arr(1 To 150, 1 To 150) As Variant
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        ' Load the array. Note the single quote in front of
        ' the parenthesis.
        For i = 1 To UBound(arr, 1)
            For j = 1 To UBound(arr, 2)
                arr(i, j) = CStr("'(" & i & "," & j & ")")
            Next j
        Next i
        
        ' Add values to the range in one step
        Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
        
        
        ' To remove the error "Number stored as text"
        Application.ErrorCheckingOptions.NumberAsText = False
        
        
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        
    End Sub
    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Note the apostrophe in the formula
        With Range("a1").Resize(150, 150)
            .FormulaR1C1 = "=""'(""&ROW()&"",""&Column()&"")"""
            .Value = .Value
        End With

  5. #5
    Thanks a lot, it worked fine!

Tags for this Thread

Posting Permissions

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