Macro Transport Data

decent_boy

Board Regular
Joined
Dec 5, 2014
Messages
130
Office Version
  1. 2016
Platform
  1. Windows
I have data sheet and there are columns of Employee ID, Date, Start, Duration, & Break I want a macro to copy data sheet data and transpose it in result sheet, same as result sheet example I am showing below.

Shift is in col D in data sheet
Break is in col E in data sheet

Book2
ABCDE
1Employee IDDateStartDurationBreak
2DX10011/23/20239:38 AM1:53:17
3DX10011/23/20231:57 PM0:39:392:25:25
4DX10011/23/20232:46 PM0:27:100:09:55
5DX10071/23/20236:04 AM0:47:320:00:00
6DX10071/23/20236:58 AM0:05:440:06:44
7DX10071/23/20237:07 AM0:46:500:03:18
8DX10071/23/20237:59 AM0:02:490:05:03
9DX10071/23/20239:09 AM0:07:391:07:50
10DX10071/23/20239:22 AM0:09:220:04:57
11DX10071/23/20231:47 PM0:43:214:15:13
Data


Book2
ABCDEFGHIJKLMNOPQR
1DATEIDStartShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShift
21/23/2023DX10019:38:18 AM1:53:172:25:250:39:390:09:550:27:100:00:000:00:000:00:000:00:000:00:000:00:000:00:000:00:000:00:000:00:00
31/23/2023DX10076:04:08 AM0:47:320:06:440:05:440:03:180:46:500:05:030:02:491:07:500:07:390:04:570:09:224:15:130:43:210:00:000:00:00
Result
 
VBA Code:
Sub Transpose_ColRows()
        Dim MyCol As New Collection
        Dim k, lr As Long
        Dim wk1 As Worksheet, wk2 As Worksheet
        Dim v
        Dim Temp
        v = Array("Date", "ID", "Start", "Shift", "Break")
        Set wk1 = Sheets("Data")                                      'change your sheet
        Set wk2 = Sheets.Add
        
        lr = wk1.Range("A" & Rows.Count).End(xlUp).Row

        With wk2
            For k = 2 To lr
                             On Error GoTo ErrorGoNext
                             MyCol.Add 1, wk1.Range("A" & k).Value & wk1.Range("B" & k).Value
                             On Error GoTo 0
                             wk1.Range("B" & k).Copy .Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                             wk1.Range("A" & k).Copy .Cells(.Range("A" & Rows.Count).End(xlUp).Row, Columns.Count).End(xlToLeft).Offset(0, 1)
                             wk1.Range("C" & k).Resize(1, 2).Copy .Cells(.Range("A" & Rows.Count).End(xlUp).Row, Columns.Count).End(xlToLeft).Offset(0, 1)
                                GoTo EarlyExit
ErrorGoNext:
                            wk1.Range("E" & k).Copy .Cells(.Range("A" & Rows.Count).End(xlUp).Row, Columns.Count).End(xlToLeft).Offset(0, 1)
                            wk1.Range("D" & k).Copy .Cells(.Range("A" & Rows.Count).End(xlUp).Row, Columns.Count).End(xlToLeft).Offset(0, 1)
                            Resume EarlyExit
EarlyExit:
            Next k
        End With
        
        lc = wk2.Cells.Find("*", LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
        
            With wk2
                .Range("A1").Resize(1, 5) = v
                .Range("D1:E1").AutoFill .Range("D1", .Cells(1, lc))
            End With
End Sub
 

Attachments

  • 1674813346512.png
    1674813346512.png
    21.5 KB · Views: 10
Upvote 1
Solution

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top