+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    10-13-2004
    Posts
    26

    Question Displaying an array as columns in a msgbox

    Hello all, I was looking around on how to create columns in a message box to display the contents of an array, but all I found was how to get them into a list box. I'm EXTREMELY close on this one.

    This code creates an array of integers from 1 to 39, then displays them in a 3 x 13 grid in a message box, i.e.:

    1 2 3
    4 5 6
    7 8 9

    and so forth. The problem I'm having is that it won't output anything after "1" in the first column, i.e.:

    1 2 3
    5 6
    8 9
    11 12
    14 15

    Here's my code:

    Code:
    Sub test()
    
    Dim a() as integer
    
    i = 39
    x = 0
    
    ReDim Preserve a(1 To i)
    
        For d = 1 To i
        
            x = x + 1
            a(x) = d
    
        Next d
    
    i1 = 1 'index 1, 2, etc.
    i2 = 2
    i3 = 3
    
    
    t = a(i1)
    m = a(i2)
    n = a(i3)
    
    
        For d = 1 To 13
            t = t & vbTab & a(i2) & vbTab & a(i3) & vbCrLf
    
            't = a(i1 + 3)
            i2 = i2 + 3
            i3 = i3 + 3
    
            Debug.Print t
    
        Next d
    
    MsgBox t
    
    End Sub
    Using t = a(i1 + 3) nets me one column of 4's. If I refer to the array directly, as in:

    t = a(i1) & vbTab & a(i2) & vbTab & a(i3) & vbCrLf

    t = a(i1 + 3)...

    Then t only retains the last 3 numbers (37, 38, 39) and displays that in the message box. I may be wrong, but I feel that this can be done. Anyone have any different ideas? Thanks in advance!

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,027
    Hi

    A couple of ways

    Code:
    Sub aaa()
      Dim a() As Integer
      Dim strr As String
      i = 39
      ReDim a(i)
      
      For x = 1 To i
        a(x) = x
      Next x
      
      For x = 1 To i Step 3
        strr = strr & a(x) & "," & a(x + 1) & "," & a(x + 2) & vbCrLf
      Next x
      
      MsgBox strr
    End Sub
    
    Sub bbb()
      Dim i As Integer
      Dim strr As String
      i = 39
      
      For x = 1 To i Step 3
        strr = strr & x & "," & x + 1 & "," & x + 2 & vbCrLf
      Next x
      
      MsgBox strr
        
    End Sub

    rylo

  3. #3
    Registered User
    Join Date
    10-13-2004
    Posts
    26
    Heh, ingenious. Never thought of just using a blank string variable. Thanks rylo!

Thread Information

Users Browsing this Thread

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

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