+ Reply to Thread
Results 1 to 8 of 8

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:
    Please Login or Register  to view this content.

    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 Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Printing a 2 dimensional array

    Perhaps switching the order of the arrays from
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    This is not tested at run-time
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  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
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Printing a 2 dimensional array

    hi, parasbshah, it can be done this way

    Please Login or Register  to view this content.

  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
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,963

    Re: Printing a 2 dimensional array

    Try:
    Please Login or Register  to view this content.
    Chr(9)=Tab
    Last edited by protonLeah; 07-09-2011 at 07:10 PM.
    Ben Van Johnson

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Printing a 2 dimensional array

    Please Login or Register  to view this content.

  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.6.0 RC 1