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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You can try this.
VBA Code:
Sub bolder()
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        Dim k, lr As Long
        Dim wk1 As Worksheet, wk2 As Worksheet
        Dim v
        v = Array("Date", "ID", "Start")
        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
                    If dic.Exists(wk1.Range("A" & k).Value) = False Then
                        dic(wk1.Range("A" & k).Value) = k
                         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)
                         
                    Else
                        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)
                    End If
            Next k
        
        End With
        
        lc = wk2.Cells.Find("*", LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
        
        With wk2
            .Range("A1").Resize(1, 3) = v
            Do
                .Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = "Shift"
                .Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = "Break"
                k = .Cells(1, Columns.Count).End(xlToLeft).Column
            Loop While k <= lc
        .Cells(1, .Cells.Find("*", LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column).EntireColumn.Delete
        End With
End Sub
 

Attachments

  • 1674589986094.png
    1674589986094.png
    19.1 KB · Views: 23
Upvote 0
It's not working cuz macs don't have dictionary and scripting runtime. You can try this one.
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
                             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
 
Upvote 0
You can try this.
VBA Code:
Sub bolder()
        Dim dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        Dim k, lr As Long
        Dim wk1 As Worksheet, wk2 As Worksheet
        Dim v
        v = Array("Date", "ID", "Start")
        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
                    If dic.Exists(wk1.Range("A" & k).Value) = False Then
                        dic(wk1.Range("A" & k).Value) = k
                         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)
                       
                    Else
                        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)
                    End If
            Next k
      
        End With
      
        lc = wk2.Cells.Find("*", LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
      
        With wk2
            .Range("A1").Resize(1, 3) = v
            Do
                .Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = "Shift"
                .Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = "Break"
                k = .Cells(1, Columns.Count).End(xlToLeft).Column
            Loop While k <= lc
        .Cells(1, .Cells.Find("*", LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column).EntireColumn.Delete
        End With
End Sub
66
 
Upvote 0
HI
I am using code for mac (Dim MyCol As New Collection) which you provided and when I add further data of date 1/24/2023, this does not copy in result sheet and macro copy only 1/23/2023 data

Book1
ABCDE
1Employee IDDateStartDurationBreak
2DX10011/23/20239:38 AM1:53
3DX10011/23/20231:57 PM0:392:25
4DX10011/23/20232:46 PM0:270:09
5DX10071/23/20236:04 AM0:470:00
6DX10071/23/20236:58 AM0:050:06
7DX10071/23/20237:07 AM0:460:03
8DX10071/23/20237:59 AM0:020:05
9DX10071/23/20239:09 AM0:071:07
10DX10071/23/20239:22 AM0:090:04
11DX10071/23/20231:47 PM0:434:15
12DX10011/24/20239:38 AM1:53
13DX10011/24/20231:57 PM0:392:25
14DX10011/24/20232:46 PM0:270:09
15DX10071/24/20236:04 AM0:470:00
16DX10071/24/20236:58 AM0:050:06
Data


Book1
ABCDEFGHIJKLMNOPQR
1DATEIDStartShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShift
21/23/2023DX10019:38 AM1:532:250:390:090:270:000:000:000:000:000:000:000:000:000:00
31/23/2023DX10076:04 AM0:470:060:050:030:460:050:021:070:070:040:094:150:430:000:00
Result
 
Upvote 0
Further result should be like this after running macro

Book1
ABCDEFGHIJKLMNOPQR
1DATEIDStartShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShiftBreakShift
21/23/2023DX10019:38 AM1:532:250:390:090:270:000:000:000:000:000:000:000:000:000:00
31/23/2023DX10076:04 AM0:470:060:050:030:460:050:021:070:070:040:094:150:430:000:00
41/24/2023DX10019:38 AM1:532:250:390:090:27
51/24/2023DX10076:04 AM0:470:060:05
Result
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
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