+ Reply to Thread
Results 1 to 12 of 12

Transpose

Hybrid View

nihitsk Transpose 03-25-2011, 03:18 AM
col12345 Re: A Challenging Transpose 03-25-2011, 07:32 AM
pike Re: A Challenging Transpose 03-25-2011, 08:13 AM
pike Re: Transpose 03-25-2011, 08:27 AM
pike Re: Transpose 03-25-2011, 08:35 AM
col12345 Re: Transpose 03-25-2011, 08:37 AM
nihitsk Re: Transpose 03-25-2011, 08:54 AM
pike Re: Transpose 03-25-2011, 08:39 AM
col12345 Re: Transpose 03-25-2011, 08:46 AM
TMS Re: Transpose 03-25-2011, 09:00 AM
col12345 Re: Transpose 03-25-2011, 03:38 PM
pike Re: Transpose 03-25-2011, 04:56 PM
  1. #1
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Question Transpose

    Holla Experts,

    I have been working on a project and this module is becoming a big roadblock..

    I have to transform the data from one format to another format so that our DBA could transform it to the database.

    I want a Generic Macro Program that could be applied to all such data...

    The number of rows and columns are variable. And there are no empty rows or columns..

    Also the header of the columns are variable..

    there is an empty column after the text field (Area) to diffrentiate it with the numeric data of different years.

    Please do see the sample sheet attached (with color coding). there are two sheets with names "Original" with the raw data and "Output" with the required conversion.

    I am really stuck and I know this is the only place wher i would get help..!!
    Attached Files Attached Files
    Last edited by nihitsk; 03-25-2011 at 09:16 AM. Reason: Solved

  2. #2
    Registered User
    Join Date
    03-24-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2000
    Posts
    36

    Re: A Challenging Transpose

    First my apologies for the style, I got started on CBM Pet basic and haven't lost the spaghetti coding bug yet.

    This works on your current data set and should work no matter how you expand the cols and rows, although I didn't test it fully.

    A few points

    1. the original data should be on a worksheet called "ORIGINAL" and the output sheet created as "output"...basically just as they are now.
    2. The header "Blank" must be in the relative place as it appears in your original.
    3. To achieve your exact result you should delete your unwanted cols in the output like "Area" or whatever.
    That's about it.


    Sub trans()



    Dim col As Integer

    Dim origrow As Integer

    Dim outrow As Integer

    Dim datcol As Integer



    outrow = 1



    label1:

    col = col + 1

    If Worksheets("ORIGINAL").Cells(1, col) = "Blank" Then GoTo label2

    datcol = col

    Worksheets("Output").Cells(1, col + 1) = Worksheets("ORIGINAL").Cells(1, col)

    GoTo label1

    label2:

    col = col + 1

    origrow = 1

    label3:

    outrow = outrow + 1

    origrow = origrow + 1

    Worksheets("Output").Cells(outrow, 1) = Worksheets("ORIGINAL").Cells(1, col)

    For x = 1 To (datcol)

    Worksheets("Output").Cells(outrow, x + 1) = Worksheets("ORIGINAL").Cells(origrow, x)



    Next x

    Worksheets("Output").Cells(outrow, x + 1) = Worksheets("ORIGINAL").Cells(origrow, col)

    If Worksheets("ORIGINAL").Cells(origrow + 1, col) <> 0 Then GoTo label3

    If Worksheets("ORIGINAL").Cells(1, col + 1) <> 0 Then GoTo label2



    End Sub
    Brief explanation how it works.
    The macro scans along the header row writing all it finds into the output until it hits "Blank"
    It will then take the value of the next col (Date) and place it in col A row1 and populate the rest of the row.
    It will then check to see if the next row has data, etc until cell is empty, it will then check to see if the next date header isn't empty and carry on.

    Hope it does the trick, but I'm sure it wouldn't need much tinkering to sort it.
    best of luck

    col

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: A Challenging Transpose

    or another way..
    Option Explicit
    Sub ptest()
    Dim xRow As Long, xCol As Long, CountCol As Long, CountRow As Long, rRow As Long
    rRow = 2
    With Sheets("ORIGINAL")
        xRow = .Cells(Rows.Count, 1).End(xlUp).Row
        xCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For CountCol = 9 To xCol
    For CountRow = 2 To xRow
     .Cells(1, CountCol).Copy Destination:=Sheets("Output").Range("A" & rRow)
     .Cells(CountRow, 1).Resize(1, 6).Copy Destination:=Sheets("Sheet1").Range("B" & rRow)
    rRow = rRow + 1
    Next
    Next
    End With
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Transpose

    better still
    Option Explicit
    Sub ptest()
    Application.ScreenUpdating = False
    Dim xRow As Long, xCol As Long, CountCol As Long, CountRow As Long, rRow As Long
    rRow = 2
    With Sheets("ORIGINAL")
        xRow = .Cells(Rows.Count, 1).End(xlUp).Row
        xCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For CountCol = 9 To xCol
    For CountRow = 2 To xRow
     .Cells(1, CountCol).Copy Destination:=Sheets("Output").Range("A" & rRow)
     .Cells(CountRow, 1).Resize(1, 6).Copy Destination:=Sheets("Output").Range("B" & rRow)
     .Cells(CountRow, CountCol).Copy Destination:=Sheets("Output").Range("H" & rRow)
    rRow = rRow + 1
    Next
    Next
    End With
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Transpose

    or even quicker
    Sub ptesti()
    Application.ScreenUpdating = False
    Dim xRow As Long, xCol As Long, CountCol As Long, CountRow As Long, rRow As Long
    rRow = 2
    With Sheets("ORIGINAL")
        xRow = .Cells(Rows.Count, 1).End(xlUp).Row
        xCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For CountCol = 9 To xCol
    For CountRow = 2 To xRow
      Sheets("Output").Range("A" & rRow).Value = .Cells(1, CountCol).Value
     Sheets("Output").Range("B" & rRow).Resize(1, 6).Value = .Cells(CountRow, 1).Resize(1, 6).Value
     Sheets("Output").Range("H" & rRow).Value = .Cells(CountRow, CountCol).Value
    rRow = rRow + 1
    Next
    Next
    End With
    Application.ScreenUpdating = True
    End Sub

  6. #6
    Registered User
    Join Date
    03-24-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2000
    Posts
    36

    Re: Transpose

    Just read your code pike and although you put me to shame (not hard really) I think the header "Blank" is quite important to the scheme as I think columns could be added or taken away, thus making "Blank" an "end of headers" marker, dates now follow.

  7. #7
    Registered User
    Join Date
    03-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    17

    Cool Re: Transpose

    Hello Guys,

    Thanks for the response.

    All of the codes worked fine for the given workbook but they are not completely generic.

    I have coded it to be generic to any kind of data with any position of the blank column (except last).

    But there is just one issue,

    The code takes a hell lot of time to execute when inserting a row.

    Pike and Col12345, please do help me make this thing a bit quicker.

    ' Macro3 Macro
    '
    
    '
    Dim x As Long
    Dim col As Long
    Dim xr As Long
    
    Application.ScreenUpdating = False
    Sheets(1).Select
        Range("A2").Select
        Selection.End(xlToRight).Select
        Selection.End(xlToRight).Select
        Range(Selection, Selection.End(xlToRight)).Select
        x = Selection.Columns.Count
        MsgBox (x)
        
        'title
        
        Rows("1:1").Select
        Selection.Copy
        Sheets(2).Select
        Rows("1:1").Select
        ActiveSheet.Paste
        
        'rows beneath title
        
        Sheets(1).Select
        Range("A2").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets(2).Select
        
        Range("A2").Select
        For col = 1 To x
         'MsgBox (x)
        'Sheets(1).Select
        'Range(Selection, Selection.End(xlToRight)).Select
        'Range(Selection, Selection.End(xlDown)).Select
        'Selection.Copy
        'Sheets(2).Select
        
        ActiveSheet.Paste
        
       Selection.End(xlToLeft).Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Select
        'Range("A15").Select
        'Sheets(1).Select
    Next col
    
    'Bring columns down
    
    'First Column
    Sheets(1).Select
        Range("A2").Select
        Selection.End(xlToRight).Select
        Selection.End(xlToRight).Select
        Range(Selection, Selection.End(xlDown)).Select
         xr = Selection.Rows.Count
        Application.CutCopyMode = False
        Selection.Copy
        Sheets(2).Select
        Range("A2").Select
        Selection.End(xlToRight).Select
        ActiveCell.Offset(0, 2).Select
    
        
        'Rest Column
      For col = 1 To x
          ActiveSheet.Paste
      Sheets(1).Select
          ActiveCell.Offset(0, 1).Select
        Range(Selection, Selection.End(xlDown)).Select
        'Application.CutCopyMode = False
        Selection.Copy
        Sheets(2).Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Select
        
        'ActiveSheet.Paste
        
        'Application.CutCopyMode = False
        Next col
        
        'insert new row
        
         Sheets(2).Select
         Range("A1").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    
    Sheets(1).Select
    Range("A2").Select
        Selection.End(xlToRight).Select
        Selection.End(xlToRight).Select
         ActiveCell.Offset(-1, 0).Select
        Selection.Copy
        Sheets(2).Select
        Range("A2").Select
        For col = 1 To xr
        ActiveSheet.Paste
           ActiveCell.Offset(1, 0).Select
           Next
           
           'Repeat for Next years
           For j = 0 To x - 2
           Sheets(1).Select
           ActiveCell.Offset(0, 1).Select
           Selection.Copy
           Sheets(2).Select
           
           For col = 1 To xr
        ActiveSheet.Paste
           ActiveCell.Offset(1, 0).Select
           'ActiveCell.Offset(1, 0).Select
           Next
                  Next
               Cells.Select
        Cells.EntireColumn.AutoFit
        Range("A2").Select
                  Selection.End(xlToRight).Select
        Selection.End(xlToRight).Select
    ActiveCell.Offset(-1, 0).Select
        Selection.ClearContents
        
        ActiveCell.FormulaR1C1 = "Volume"
          ActiveCell.Offset(0, 1).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.ClearContents

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Transpose

    i just copied your idea in the last post

  9. #9
    Registered User
    Join Date
    03-24-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2000
    Posts
    36

    Re: Transpose

    sorry my knowledge of VBA is very limited and most of your code goes straight over my head.

    My apologies for doubting you.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,119

    Re: Transpose

    Slight modification to Pike's code to pick up on the blank column:

    Sub ptest()
    Application.ScreenUpdating = False
    Dim xRow As Long, xCol As Long, CountCol As Long, CountRow As Long, rRow As Long, BlankCol As Long
    rRow = 2
    With Sheets("ORIGINAL")
        BlankCol = Application.WorksheetFunction.Match("Blank", .Rows(1))
        xRow = .Cells(Rows.Count, 1).End(xlUp).Row
        xCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        For CountCol = BlankCol + 1 To xCol
            For CountRow = 2 To xRow
             .Cells(1, CountCol).Copy Destination:=Sheets("Output").Range("A" & rRow)
             .Cells(CountRow, 1).Resize(1, 6).Copy Destination:=Sheets("Output").Range("B" & rRow)
             .Cells(CountRow, CountCol).Copy Destination:=Sheets("Output").Range("H" & rRow)
            rRow = rRow + 1
            Next
        Next
    End With
    Application.ScreenUpdating = True
    End Sub

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  11. #11
    Registered User
    Join Date
    03-24-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2000
    Posts
    36

    Re: Transpose

    sorry to disappoint, but my VBA skills are limited, pike and TMshucks are clearly your best bet.

  12. #12
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Transpose

    TMShucks thanks for the correction

    this one maybe faster but the problem with loops is that they are slow

    Sub ptesti()
    Application.ScreenUpdating = False
    Dim xRow As Long, xCol As Long, CountCol As Long, CountRow As Long, rRow As Long
    rRow = 2
    With Sheets("ORIGINAL")
        xRow = .Cells(Rows.Count, 1).End(xlUp).Row
        xCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For CountCol = 9 To xCol
    For CountRow = 2 To xRow
      Sheets("Output").Range("A" & rRow).Value = .Cells(1, CountCol).Value
     Sheets("Output").Range("B" & rRow).Resize(1, 6).Value = .Cells(CountRow, 1).Resize(1, 6).Value
     Sheets("Output").Range("H" & rRow).Value = .Cells(CountRow, CountCol).Value
    rRow = rRow + 1
    Next
    Next
    End With
    Application.ScreenUpdating = True

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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