Solved

Convert Columns to Rows

Posted on 2012-03-28
11
228 Views
Last Modified: 2012-08-13
Hello:

I am trying to convert data that looks like this:

Oct-2010      Oct-2010      Nov-2010      Nov-2010      Dec-2010      Dec-2010      Jan-2011      Jan-2011
Score      N      Score      N      Score      N      Score      N
 67.3      11743       45.2      9834       71.4      10253       87.1      14374

To look like this:

MthYr            Score           N
Oct-2010        67.3            11743
Nov-2010       45.2              9834
Dec-2010       71.4            10253
Test.xlsx
0
Comment
Question by:RecipeDan
  • 5
  • 5
11 Comments
 
LVL 12

Expert Comment

by:kgerb
ID: 37778370
1.  Copy data in chunks (for example, all the dates)
2.  paste special, select transpose check box, click ok
3.  Ctrl+select all Scores, copy, paste special, transpose, ok
4.  Ctrl+select all N-values, copy, paste special, transpose, ok

Will that work for you?

Kyle
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37778378
Hi, RecipeDan.

As it stands, it's very straightforward. However, I'm assuming that reality is more complicated - lots more columns and/or rows, perhaps? So, how many of each?

Thanks,
Brian.
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 37778412
@Brian: Yes its a little more complicated. I have 24 columns (12 months + 12 N Values) and the number of rows will very based on content.

@Kyle: I am looking for a macro that the users can run. I am working with users who have a very basic knowledge of Excel. The similiar the better.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37778804
RecipeDan,

OK, let's start with the attached. The macro is...
Option Explicit

Sub Reformat_Data()
Dim xInput As Worksheet
Dim xOutput As Worksheet
Dim xLast_Row As Long
Dim xOut_Row As Long
Dim i As Long, j As Long

Set xInput = Sheets("Input")

xLast_Row = xInput.Range("A1").SpecialCells(xlLastCell).Row
If xLast_Row < 3 Then
    MsgBox ("No Data found. Run cancelled.")
    Exit Sub
End If

Application.ScreenUpdating = False
    Set xOutput = Sheets.Add
    
    xOutput.Range("A1:D1") = Array("Row", "MthYr", "Score", "N")
    xOut_Row = 1
    
    With xInput
        For i = 3 To xLast_Row
            For j = 2 To 24 Step 2
                xOut_Row = xOut_Row + 1
                xOutput.Cells(xOut_Row, 1) = .Cells(i, 1)
                xOutput.Cells(xOut_Row, 2) = .Cells(1, j)
                xOutput.Cells(xOut_Row, 3) = .Cells(i, j)
                xOutput.Cells(xOut_Row, 4) = .Cells(i, j + 1)
            Next
        Next
    
    End With
    
    xOutput.Columns("B:B").NumberFormat = "mmm-yyyy"
    
Application.ScreenUpdating = True
    
End Sub

Open in new window

Regards,
Brian.
Test-V3.xlsm
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 37778988
Hi Brian:

It looks good. It is exactly what I am looking for. A couple questions, do I have to put the letters Column A? If I don't have 24 columns, do I change the 24 to how ever many columns I have?

Thanks
Dan
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 37779210
RecipeDan,

do I have to put the letters Column A?
Nope. I've taken them out in the attached.

If I don't have 24 columns, do I change the 24 to how ever many columns I have?
Nearly - one less! (So, V4 uses 23 as there are 24 columns.)

Regards,
Brian.Test-V4.xlsm
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 37779300
Hi Brain:

What if I have 6 months of data (12 columns)? Also I thought of something else past data from prior years has fields before the actual data. I attached an example.

I am trying to think of all scenarios while I have this question open.

Dan
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 37779308
here is the example
TestBook1.xlsx
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37779528
RecipeDan,

What if I have 6 months of data (12 columns)?
Then you use 11. However, this has been overtaken by ...
past data from prior years has fields before the actual data.
It depends what you want to do with the extra fields - simply ignore them or include them as I did with "Row" in V3?
I am trying to think of all scenarios while I have this question open.
I understand why you think that's useful but it tends to waste both our time! Even if you find you've forgotten something then
(a) Post in the closed question. (Not relevant for all Experts, but I keep an eye on "my" closed questions for at least a couple of weeks.)
(b) Ask a new question, including a link to the closed one.

Regards,
Brian.
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 37783284
OK sounds good..Thank you for your help
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37784256
RecipeDan,

Thanks, but what about columns A and B?!

Regards,
Brian.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now