+ Reply to Thread
Results 1 to 7 of 7

Taking data in rows and stacking into columns

Hybrid View

jcoe Taking data in rows and... 03-20-2013, 10:22 AM
arlu1201 Re: Taking data in rows and... 03-20-2013, 11:00 AM
Eliat Re: Taking data in rows and... 03-20-2013, 11:56 AM
arlu1201 Re: Taking data in rows and... 03-20-2013, 01:35 PM
jcoe Re: Taking data in rows and... 03-20-2013, 05:35 PM
arlu1201 Re: Taking data in rows and... 03-21-2013, 10:54 AM
Eliat Re: Taking data in rows and... 03-22-2013, 05:50 AM
  1. #1
    Registered User
    Join Date
    03-19-2013
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    1

    Taking data in rows and stacking into columns

    Hi,

    I'm a new member to the forum and hoping someone may be able to help me out with this question.

    I have a workbook that compiles data by Employee ID in a row format, meaning that all data for that employee is in one, single row. However, to upload this data into another system I have to stack some data into multiple rows, then move the next employee and so on.

    To get a better understanding, please take a look at the attached file. I have one tab that shows how the data is originally compiled, then the 2nd tab shows how the import file should look. I would like to have excel take the 1 row of original data and stack it into the 3 rows needed for the import.

    Please post any questions and I'll answer as soon as possible. Thanks to everyone in the group for your assistance.
    Attached Files Attached Files

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Taking data in rows and stacking into columns

    Try this code
    Sub transpose_data()
    Dim ws As Worksheet
    Dim i As Long, lrow As Long, lastrow As Long
    
    Application.ScreenUpdating = False
    
    Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = "Sheet1"
    Set ws = Worksheets("Sheet1")
    ws.Range("A1:S1").Value = Split("Date,Emp ID,EmplName,Home,Status1,Status2,Project,SSC,WrkPkg,Facility,R/O/S,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Total", ",")
    
    With Worksheets(1)
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
            .Range("A" & i & ":R" & i).Copy ws.Range("A" & lastrow + 1)
            ws.Range("S" & lastrow + 1).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
            lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
            .Range("A" & i & ":J" & i).Copy ws.Range("A" & lastrow + 1)
            .Range("S" & i & ":Z" & i).Copy ws.Range("K" & lastrow + 1)
            ws.Range("S" & lastrow + 1).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
            lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
            .Range("A" & i & ":J" & i).Copy ws.Range("A" & lastrow + 1)
            .Range("AA" & i & ":AH" & i).Copy ws.Range("K" & lastrow + 1)
            ws.Range("S" & lastrow + 1).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
        Next i
        .Cells.EntireColumn.AutoFit
        .Rows(1).Font.Bold = True
    End With
    
    MsgBox "Done"
    
    Application.ScreenUpdating = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    03-04-2013
    Location
    Newcastle
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Taking data in rows and stacking into columns

    Go with Arlu's code so much simpler haha

    Hopefully this should work.

    Providing that you do not input any more data horizontally :P

    Sub Macro4()
    '
    ' Macro4 Macro
    '
    
    Application.ScreenUpdating = False
        Sheets("How i receive data").Select
        Sheets.Add
        ActiveSheet.Name = "Compiled Records"
        ActiveCell.FormulaR1C1 = "Date"
        Range("B1").Select
        ActiveCell.FormulaR1C1 = "Emp ID"
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "EmplName"
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "Home"
        Range("E1").Select
        ActiveCell.FormulaR1C1 = "Status1"
        Range("F1").Select
        ActiveCell.FormulaR1C1 = "Status2"
        Range("G1").Select
        ActiveCell.FormulaR1C1 = "Project"
        Range("H1").Select
        ActiveCell.FormulaR1C1 = "SSC"
        Range("I1").Select
        ActiveCell.FormulaR1C1 = "WrkPkg"
        Range("J1").Select
        ActiveCell.FormulaR1C1 = "Facility"
        Range("K1").Select
        ActiveCell.FormulaR1C1 = "R/O/S"
        Range("L1").Select
        ActiveCell.FormulaR1C1 = "Saturday"
        Range("M1").Select
        ActiveCell.FormulaR1C1 = "Sunday"
        Range("N1").Select
        ActiveCell.FormulaR1C1 = "Monday"
        Range("O1").Select
        ActiveCell.FormulaR1C1 = "Tuesday"
        Range("P1").Select
        ActiveCell.FormulaR1C1 = "Wednesday"
        Range("Q1").Select
        ActiveCell.FormulaR1C1 = "Thursday"
        Range("R1").Select
        ActiveCell.FormulaR1C1 = "Friday"
        Range("S1").Select
        ActiveCell.FormulaR1C1 = "Total"
        Range("S2").Select
        ActiveCell.FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
        Range("S2").Select
        Selection.AutoFill Destination:=Range("S2:S472"), Type:=xlFillDefault
        Range("S2:S472").Select
        Range("S2").Select
        
    Sheets("How I receive data").Select
        Columns("AJ").ClearContents
        Range("B2:B4").Select
        Range("B1:B4").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
            "B3:B4"), CopyToRange:=Range("AJ1"), Unique:=True
        ActiveWindow.SmallScroll ToRight:=3
        Columns("AJ:AJ").Select
        
     For Each c In Range("AJ2", Range("AJ" & Rows.Count).End(xlUp))
            With Range("A1:T2500")
                .AutoFilter Field:=2, Criteria1:=c.Value
      Range("A2:AH2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
                Sheets("Compiled Records").Select
       Range("U2").Select
       Do
        If (ActiveCell.Value = "") = False Then
        ActiveCell.Offset(1, 0).Select
        End If
        Loop Until ActiveCell.Value = ""
        
       Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.Copy
        Selection.Offset(1, 0).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Selection.Offset(1, 0).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(-2, -20).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(1, -17).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[28]"
        ActiveCell.Offset(1, -17).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[20]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        ActiveCell.Offset(0, 1).Select
        ActiveCell.FormulaR1C1 = "=RC[36]"
        Sheets("How i receive data").Select
                End With
        Next c
    Sheets("Compiled Records").Select
    Range("A1").Select
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by Eliat; 03-20-2013 at 11:58 AM.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Taking data in rows and stacking into columns

    Eliat,

    When i just started with VBA, i also used to code using the Select and Activate statements. You can do away with it. Just go through my code and see how it works.

  5. #5
    Registered User
    Join Date
    03-19-2013
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Taking data in rows and stacking into columns

    Arlu,

    Thanks for the code, however I cannot get it to work. When I run the Macro Excel locks up and I have force close the program to continue. Any ideas?

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Taking data in rows and stacking into columns

    Did you get any popup to enable macros?

  7. #7
    Registered User
    Join Date
    03-04-2013
    Location
    Newcastle
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Taking data in rows and stacking into columns

    Jcoe,

    I would suggest going into the VBA editor and by using F8 going through the process step by step to find the part of the code that breaks your computer.

    Hi Arlu,

    For some reason
    .Cells.EntireColumn.Autofit
    Doesn't work for me.

    This addition would seem to fix that problem on Excel 2010:

    Sub transpose_data()
    Dim ws As Worksheet
    Dim i As Long, lrow As Long, lastrow As Long
    
    Application.ScreenUpdating = False
    
    Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = "Sheet1"
    Set ws = Worksheets("Sheet1")
    ws.Range("A1:S1").Value = Split("Date,Emp ID,EmplName,Home,Status1,Status2,Project,SSC,WrkPkg,Facility,R/O/S,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Total", ",")
    
    With Worksheets(1)
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
            .Range("A" & i & ":R" & i).Copy ws.Range("A" & lastrow + 1)
            ws.Range("S" & lastrow + 1).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
            lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
            .Range("A" & i & ":J" & i).Copy ws.Range("A" & lastrow + 1)
            .Range("S" & i & ":Z" & i).Copy ws.Range("K" & lastrow + 1)
            ws.Range("S" & lastrow + 1).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
            lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
            .Range("A" & i & ":J" & i).Copy ws.Range("A" & lastrow + 1)
            .Range("AA" & i & ":AH" & i).Copy ws.Range("K" & lastrow + 1)
            ws.Range("S" & lastrow + 1).FormulaR1C1 = "=SUM(RC[-7]:RC[-1])"
        Next i
        Range("A1").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.EntireColumn.AutoFit
        .Rows(1).Font.Bold = True
    End With
    
    MsgBox "Done"
    
    Application.ScreenUpdating = True
    
    End Sub

+ 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