+ Reply to Thread
Results 1 to 3 of 3

Transposing columns to rows

Hybrid View

sunitagadapu Transposing columns to rows 06-06-2009, 12:33 AM
StephenR re: Transposing columns to... 06-06-2009, 07:41 AM
JBeaucaire re: Transposing columns to... 06-06-2009, 08:54 AM
  1. #1
    Registered User
    Join Date
    06-06-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    1

    Transposing columns to rows

    Dear Excel Gurus,

    I am quiet new to this world and am having some problems in achieving a particular requirement that i have.

    My Problem
    -----------
    I have a worksheet in which there are 2 columns, one containing the field names and the other containing the values.
    Example
     Column1            Column2
     --------------------------
     Name                John
     Age                 23
     City                London
     Country             UK
     Name                Sunita
     Age                 23
     City                Bangalore
     Country             India
    I want to move this to another worksheet and it should look in the following manner
     Name      Age     City       Country
     -------------------------------------
     John      23      London     UK
     Sunita    23      Bangalore  India
    Can you please let me know how i can achive this.

    To add to my problems the field names will not be constant i.e Column 1 might have 5 occuances of the set Name,Age,City, COuntry and can also ahve Name,Age,Country

    How can i handle this.

    Thanks in advance!!

    Sunita
    Last edited by Leith Ross; 06-06-2009 at 03:30 PM. Reason: Made tables more readable

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    re: Transposing columns to rows

    Will it always have Name, Age and Country or could they be missing too? If so, which?

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    re: Transposing columns to rows

    This little macro will create an "Output" list sheet and populate it based on the field names in column A and the values in column B. The field names DO have to be accurate.
    Option Explicit
    
    Sub TransposeList()
    Dim LR As Long, NR As Long, i As Long
    Dim wsI As Worksheet, wsO As Worksheet
    Set wsI = ActiveSheet
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    
    'Setup New Sheet
        Sheets.Add.Name = "Output" & Sheets.Count
        Set wsO = ActiveSheet
        Range("A1") = "Name"
        Range("B1") = "Age"
        Range("C1") = "City"
        Range("D1") = "Country"
        
        With Range("A1:D1")
            .Font.Bold = True
            .Borders(xlEdgeBottom).LineStyle = xlContinuous
            .Borders(xlEdgeBottom).Weight = xlMedium
        End With
        
        Range("A2").Select
        ActiveWindow.FreezePanes = True
        NR = 1
    
    'Tranpose old data
        wsI.Activate
    
        For i = 1 To LR
            Select Case Cells(i, "A").Value
                Case "Name"
                    NR = NR + 1
                    wsO.Cells(NR, "A") = Cells(i, "B")
                Case "Age"
                    wsO.Cells(NR, "B") = Cells(i, "B")
                Case "City"
                    wsO.Cells(NR, "C") = Cells(i, "B")
                Case "Country"
                    wsO.Cells(NR, "D") = Cells(i, "B")
                Case Else
                    'do nothing
            End Select
        Next i
        
    wsO.Activate
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by JBeaucaire; 06-06-2009 at 08:56 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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