+ Reply to Thread
Results 1 to 4 of 4

VBA Matrix Conversion to Columns

Hybrid View

  1. #1
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    60

    VBA Matrix Conversion to Columns

    Hello,

    I am trying to figure out something this seems like it would be easy, but am having no luck. I need to take a matrix filled with data (the dimensions of the matrix are dynamic based on the data refresh) and convert it to columns as such:

    A B C
    A 1
    B .4 1
    C .3 .2 1

    The matrix will actually be a Correlation table. So what I need to figure out is how to make three columns that pairs the data, essentially converting it from a matrix to columns like such:

    A B C (<- column headings)
    1 A A 1
    2 A B .4
    3 A C .3
    4 B A .4
    5 B B 1
    6 B C .2
    7 C A .3
    8 C B .2
    9 C C 1

    Any ideas or suggestions would be much appreciated. Thank you.
    Last edited by learning_vba; 11-24-2011 at 05:11 PM. Reason: Solved

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Matrix Conversion to Columns

    Sub snb()
     sn = Cells(1).CurrentRegion
    
     For j = 2 To UBound(sn)
      For jj = 2 To UBound(sn, 2)
       c01 = c01 & vbCr & sn(j, 1) & "|" & sn(1, jj) & "|" & sn(j, jj)
      Next
     Next
     sn = Split(Mid(c01, 2), vbCr)
    
     Cells(1, 10).Resize(UBound(sn)) = Application.Transpose(sn)
     Columns(10).TextToColumns , , , , False, False, False, False, True, "|"
    End Sub
    Attached Files Attached Files



  3. #3
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: VBA Matrix Conversion to Columns

    Quote Originally Posted by snb View Post
    Sub snb()
     sn = Cells(1).CurrentRegion
    
     For j = 2 To UBound(sn)
      For jj = 2 To UBound(sn, 2)
       c01 = c01 & vbCr & sn(j, 1) & "|" & sn(1, jj) & "|" & sn(j, jj)
      Next
     Next
     sn = Split(Mid(c01, 2), vbCr)
    
     Cells(1, 10).Resize(UBound(sn)) = Application.Transpose(sn)
     Columns(10).TextToColumns , , , , False, False, False, False, True, "|"
    End Sub
    Thank you so much- this works perfectly!

  4. #4
    Registered User
    Join Date
    10-21-2012
    Location
    Iran;Isfahan
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    1

    Re: VBA Matrix Conversion to Columns

    Thank you. it was perfect. but if you want to see the last data in matrix you should replase Resize(UBound(sn)) with Resize(UBound(sn) + 1) .
    Last edited by lale_s; 10-21-2012 at 01:28 AM.

+ 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