+ Reply to Thread
Results 1 to 8 of 8

Thread: Printing a 2 dimensional array

  1. #1
    Registered User
    Join Date
    07-09-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Question Printing a 2 dimensional array

    Hello,

    I am fairly new at VBA Programming for Excel. I would like to print a 2D array (matrix if you will)
    to a text file. The 2D array is variant in type as it consists of both string and text. I would like the
    array to look as below:

    $ CASE COLD NOISE COLD NOISE COLD SCUFF DURABILITY HOTSCUFF
    SPEED 1800 2800 4200 6200 6200
    BHPE 5000.0 10000.0 36000.0 65900.0 65900.0
    FHPE 3000.0 5000.0 20000.0 70000.0 70000.0

    I am using the follwing code:
       For iRow = 1 4
          For iCol = 1 To 6  
             Print #IfileNum, LoopData(iRow, iCol)   ' Data is already stored in LoopData matrix
          Next iCol 
       Next iRow

    However using this code prints in successive lines as below:

    $ CASE
    COLD NOISE
    COLD NOISE
    COLD SCUFF
    .....
    .....
    And So on instead of the column format as I want. How do I get VBA to print as shown above in a column format.

    Thanks

    Paras
    Last edited by Leith Ross; 07-09-2011 at 03:45 PM. Reason: Added Code Tags

  2. #2
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Printing a 2 dimensional array

    Perhaps switching the order of the arrays from
    For iRow = 1 4
          For iCol = 1 To 6  
             Print #IfileNum, LoopData(iRow, iCol)   ' Data is already stored in LoopData matrix
          Next iCol 
       Next iRow
    to
    Sub parasbshah()
        For iCol = 1 To 6
            For iRow = 1 To 4
                Print #IfileNum, LoopData(iRow, iCol)   ' Data is already stored in LoopData matrix
            Next iCol
        Next iRow
    End Sub
    This is not tested at run-time
    Please leave a message after the beep!

  3. #3
    Registered User
    Join Date
    07-09-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Printing a 2 dimensional array

    Mordred,

    Thanks for the suggestion. Howver that did not work. What it just did was print the first column of the array in successive lines and then started the second column and so on (as shown below)

    $ CASE
    SPEED
    BHPE
    FHPE
    COLD NOISE
    1800
    5000
    3000
    ....
    ....
    And so on

  4. #4
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Printing a 2 dimensional array

    hi, parasbshah, it can be done this way

    Sub test()
    x = [a4:b9] 'A4:B9 has data table
    Set fs = CreateObject("Scripting.FileSystemObject"): Set a = fs.CreateTextFile("c:\testfile.txt", True)
    For i = 1 To UBound(x): a.WriteLine x(i, 1) & "   " & x(i, 2): Next: a.Close
    End Sub

  5. #5
    Registered User
    Join Date
    07-09-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Printing a 2 dimensional array

    Hi WaterSev,

    Thanks for your suggestion. However in my case the array "x" is variable in size depending on the number of Columns entered by the user. So the statement

    For i = 1 To UBound(x): a.WriteLine x(i, 1) & " " & x(i, 2): Next: a.Close

    will not work for me since I would not know beforehand how many times to concatenate x(irow,icol). I need a logic which will automatically adjust to the number of columns in the array x.

    Do you have any suggestions?

    Thanks

    Paras

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7/winXP
    Posts
    2,180

    Re: Printing a 2 dimensional array

    Try:
    Option Explicit
    
    Sub parasbshah2()
        Dim LineOut As String, _
            iCol    As Long, _
            iRow    As Long, _
            fs, _
            FileOut
            
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set FileOut = fs.CreateTextFile("c:\testfile.txt", True)
        For iRow = 1 To 4
            For iCol = 1 To 5
                LineOut = LineOut & loopdata(iRow, iCol) & Chr(9)
            Next iCol
            LineOut = LineOut & loopdata(iRow, 6)        
            FileOut.WriteLine LineOut
            LineOut = ""
        Next iRow
    End Sub
    Chr(9)=Tab
    Last edited by protonLeah; 07-09-2011 at 07:10 PM.
    ---
    Ben Van Johnson

  7. #7
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Printing a 2 dimensional array

    Sub test()
    x = [a4:c9] 'A4:C9 has data table
    Set fs = CreateObject("Scripting.FileSystemObject"): Set a = fs.CreateTextFile("c:\testfile.txt", True)
    delim = "   "
    For i = 1 To UBound(x)
        For n = 1 To UBound(x, 2)
            temp = temp & x(i, n) & delim
        Next
        a.WriteLine Left(temp, Len(temp) - 3): temp = ""
    Next: a.Close
    End Sub

  8. #8
    Registered User
    Join Date
    07-09-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Printing a 2 dimensional array

    Hi ProtonLeah & watersev

    I tried the suggestions that you gave me and it worked like a charm.

    Thanks a lot Guys !!!!

+ Reply to Thread

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