Consulting

Results 1 to 9 of 9

Thread: Solved: Transpose table of data

  1. #1
    VBAX Regular
    Joined
    Jan 2010
    Location
    Des Moines, Iowa
    Posts
    13
    Location

    Solved: Transpose table of data

    I have a table of data that needs to be transposed in a particular way. This table could have up to 1500 rows of data very similar to the example shown in the attached Excel file. Could someone help me with a VBA Macro to help accomplish this? It could be a huge time saver for me. Please see the attached file for more information.

    Thank you very much.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long, j As Long
    Dim LastRow As Long
    Dim LastCol As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = LastRow To 3 Step -1

    LastCol = .Cells(i, .Columns.Count).End(xlToLeft).Column
    .Rows(i + 1).Resize(LastCol - 1).Insert
    For j = LastCol To 2 Step -1

    .Cells(i, "A").Copy .Cells(i + j - 1, "A")
    .Cells(i, j).Copy .Cells(i + j - 1, "B")
    Next j

    .Rows(i).Delete
    Next i

    .Rows(1).Delete
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    .Range("C1").Resize(, LastCol - 2).ClearContents
    .Range("A1").Value = "Payment"
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Jan 2010
    Location
    Des Moines, Iowa
    Posts
    13
    Location

    Run Time Error 1004

    Thank you. This works very good. I am getting a Run Time Error 1004 but it doens't seem to affect anything. Anything I can do to correct the error?

    Thanks again for the quick reply.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Where does the error occur?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Jan 2010
    Location
    Des Moines, Iowa
    Posts
    13
    Location
    When I debug, it stops at this line of code:


    .Rows(i + 1).Resize(LastCol - 1).Insert

    Thanks again for your help.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have just run it again on the workbook that you posted and I get no error, so you need to post the offending workbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Jan 2010
    Location
    Des Moines, Iowa
    Posts
    13
    Location

    Transpose Table - Run Time Error

    I attached a copy of the spreadsheet with an example table to transpose. The VBA code is included. When I run it I get a run time error # 1004. Otherwise the macro seems to be doing what it is supposed to.

    Thanks again for your help.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I had assumed that those two blank rows at the top were not in the real spreadsheet.

    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long, j As Long
    Dim LastRow As Long
    Dim LastCol As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = LastRow To 5 Step -1

    LastCol = .Cells(i, .Columns.Count).End(xlToLeft).Column
    .Rows(i + 1).Resize(LastCol - 1).Insert
    For j = LastCol To 2 Step -1

    .Cells(i, "A").Copy .Cells(i + j - 1, "A")
    .Cells(i, j).Copy .Cells(i + j - 1, "B")
    Next j

    .Rows(i).Delete
    Next i

    .Rows(3).Delete
    LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column
    .Range("C3").Resize(, LastCol - 2).ClearContents
    .Range("A3").Value = "Payment"
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Regular
    Joined
    Jan 2010
    Location
    Des Moines, Iowa
    Posts
    13
    Location
    Ok. Thanks. That takes care of the problem. You were a big help!

Posting Permissions

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