[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert Columns to Rows

Posted on 2012-03-28
11
Medium Priority
?
241 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 26

Accepted Solution

by:
redmondb earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

873 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