Looking for code to transpose data

Drew

Board Regular
Joined
Feb 18, 2002
Messages
187
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello all,
I'm hoping someone can help me out utilizing code to transpose monthly data. I've got five years worth of data sorted by year but the months are spread across columns. I would like to have one column for amount and a new column to distinguish what month that was transposed.
I'm sure I may not be explaining it properly, but hopefully the attached can help out....
Thanks,
Drew
Book1
ABCDEFGHIJ
1data in the master sheet
2companyacct_norollup_cdrollup_lnamereclass_indyearmeb_janmeb_febmeb_maretc..
3001000999990A9%%%0A1Drew Acct 0YY2000(2.57)(1.46)(149.45)
4001000999991A9%%%0A1Drew Acct 1YY2001448.6215,648.49224.04
5001000999992A9%%%0A1Drew Acct 2YY2002(102.28)(766.17)(8,954.02)
6Data goes down approx 3k lines (approx 400 rows per year) sorted by year
7
8
9copy data to another worksheet as follows..
10companyacct_norollup_cdrollup_lnamereclass_indyearmebmth
11001000999990A9%%%0A1Drew Acct 0YY2000(2.57)meb_jan
12001000999991A9%%%0A1Drew Acct 1YY2001448.62meb_jan
13001000999992A9%%%0A1Drew Acct 2YY2002(102.28)meb_jan
14001000999990A9%%%0A1Drew Acct 0YY2000(1.46)meb_feb
15001000999991A9%%%0A1Drew Acct 1YY200115,648.49meb_feb
16001000999992A9%%%0A1Drew Acct 2YY2002(766.17)meb_feb
17001000999990A9%%%0A1Drew Acct 0YY2000(149.45)meb_mar
18001000999991A9%%%0A1Drew Acct 1YY2001224.04meb_mar
19001000999992A9%%%0A1Drew Acct 2YY2002(8,954.02)meb_mar
Sheet2
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this :-
Code:
'========================================================
'- TRANSFER DATA COLUMN-WISE TO ROW-WISE
'- Brian Baulsom May 2005
'========================================================
Option Base 1
Sub test()
    Dim FromSheet As Worksheet  ' called "Sheet1"
    Dim FromRow As Long
    Dim NumberOfMonths As Integer   ' ** amend below
    Dim MonthName As Variant        ' ** amend below
    Dim ThisMonth As String
    Dim FromCol As Long
    Dim ThisValue As Double
    Dim Col1, Col2, Col3, Col4, Col5, Col6
    '--
    Dim ToSheet As Worksheet    ' called "New"
    Dim ToRow As Long
    Dim ToCol As Long
    Dim m As Integer
    '-----------------------------------------------------
    Application.Calculation = xlCalculationManual
    Set FromSheet = Worksheets("Sheet1")               '**
    FromRow = 2
    NumberOfMonths = 3                                 '**
    MonthName = Array("meb_jan", "meb_feb", "meb_mar") '**
    Set ToSheet = Worksheets("new")                    '**
    ToRow = 2
    '-----------------------------------------------------
    '- do down rows
    While FromSheet.Cells(FromRow, 1).Value <> ""
        Col1 = FromSheet.Cells(FromRow, 1).Value
        Col2 = FromSheet.Cells(FromRow, 2).Value
        Col3 = FromSheet.Cells(FromRow, 3).Value
        Col4 = FromSheet.Cells(FromRow, 4).Value
        Col5 = FromSheet.Cells(FromRow, 5).Value
        Col6 = FromSheet.Cells(FromRow, 6).Value
        '- go across columns * transfer data
        For m = 1 To NumberOfMonths
            ThisMonth = MonthName(m)
            FromCol = m + 6
            ThisValue = FromSheet.Cells(FromRow, FromCol).Value
            If ThisValue <> 0 Then
                ToSheet.Cells(ToRow, 1).Value = Col1
                ToSheet.Cells(ToRow, 2).Value = Col2
                ToSheet.Cells(ToRow, 3).Value = Col3
                ToSheet.Cells(ToRow, 4).Value = Col4
                ToSheet.Cells(ToRow, 5).Value = Col5
                ToSheet.Cells(ToRow, 6).Value = Col6
                ToSheet.Cells(ToRow, 7).Value = ThisValue
                ToSheet.Cells(ToRow, 8).Value = ThisMonth
                ToRow = ToRow + 1
            End If
        Next
        FromRow = FromRow + 1
    Wend
    MsgBox ("Done.")
    Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,874
Members
452,949
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top