Transposing data to a single row

Mega_man87

New Member
Joined
Sep 24, 2022
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. MacOS
Hello All,

I am struggling to create a Macro that successfully transpose data into a required format. I have attached the initial data and desired format data below. I would like the macro loop through Cell C (Company name) in Sheet "Raw Data" and transpose the information into the required layout shown in Sheet "Results", with each company on a single row.

In Sheet "Results", there is a blank column after each date (and its data) and at the end of the 5th day in that week, a new week is registered and the layout continues.

Please note:
- Weeks will always have 5 days
- If more companies are added to the list, I was like it t follow the same format
- The number of weeks will always be 3 in this report


I would appreciate some help with this so much. Thank you in advance!

Initial Data:

Excel.xlsx
ABCDEF
1Week NumberDateCompanyHighest SalesStaff NameStaff Number
2131/10/2022Apple147Giada Francis1022
301/11/2022Apple107Nathaniel Figueroa9303
402/11/2022Apple170John Doe1045
503/11/2022Apple141Giada Francis1022
604/11/2022Apple113Nathaniel Figueroa9303
7207/11/2022Apple121John Doe1045
808/11/2022Apple192Nathaniel Figueroa9303
909/11/2022Apple62John Doe1045
1010/11/2022Apple61Nathaniel Figueroa9303
1111/11/2022Apple146Giada Francis1022
12314/11/2022Apple162John Doe1045
1315/11/2022Apple85Giada Francis1022
1416/11/2022Apple95John Doe1045
1517/11/2022Apple168Giada Francis1022
1618/11/2022Apple187Nathaniel Figueroa9303
17131/10/2022Microsoft127Lilian Carpenter22332
1801/11/2022Microsoft126Ryan Kaur31828
1902/11/2022Microsoft111Lilian Carpenter22332
2003/11/2022Microsoft94Annette Williams21939
2104/11/2022Microsoft136Lilian Carpenter22332
22207/11/2022Microsoft114Annette Williams21939
2308/11/2022Microsoft198Ryan Kaur31828
2409/11/2022Microsoft59Annette Williams21939
2510/11/2022Microsoft181Lilian Carpenter22332
2611/11/2022Microsoft70Ryan Kaur31828
27314/11/2022Microsoft73Annette Williams21939
2815/11/2022Microsoft90Lilian Carpenter22332
2916/11/2022Microsoft137Annette Williams21939
3017/11/2022Microsoft64Ryan Kaur31828
3118/11/2022Microsoft179Lilian Carpenter22332
32131/10/2022Samsung130Troy Davis123334
3301/11/2022Samsung144Jean-Luc Chester32454
3402/11/2022Samsung139Dione Hobbs34474
3503/11/2022Samsung152Jean-Luc Chester32454
3604/11/2022Samsung86Dione Hobbs34474
37207/11/2022Samsung170Troy Davis123334
3808/11/2022Samsung158Jean-Luc Chester32454
3909/11/2022Samsung145Dione Hobbs34474
4010/11/2022Samsung119Troy Davis123334
4111/11/2022Samsung63Dione Hobbs34474
42314/11/2022Samsung100Jean-Luc Chester32454
4315/11/2022Samsung119Troy Davis123334
4416/11/2022Samsung107Jean-Luc Chester32454
4517/11/2022Samsung172Dione Hobbs34474
4618/11/2022Samsung121Troy Davis123334
Raw Data


Desired Format of Data:

Excel.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZ
1CompanyWeek NumberDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesWeek NumberDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesWeek NumberDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number Sales
2Apple131/10/2022Giada Francis102214701/11/2022Nathaniel Figueroa930310702/11/2022John Doe104517003/11/2022Giada Francis102214104/11/2022Nathaniel Figueroa9303113207/11/2022John Doe104512108/11/2022Nathaniel Figueroa930319209/11/2022John Doe10456210/11/2022Nathaniel Figueroa93036111/11/2022Giada Francis1022146314/11/2022John Doe104516215/11/2022Giada Francis10228516/11/2022John Doe10459517/11/2022Giada Francis102216818/11/2022Nathaniel Figueroa9303187
3Microsoft131/10/2022Lilian Carpenter2233212701/11/2022Ryan Kaur3182812602/11/2022Lilian Carpenter2233211103/11/2022Annette Williams219399404/11/2022Lilian Carpenter22332136207/11/2022Annette Williams2193911408/11/2022Ryan Kaur3182819809/11/2022Annette Williams219395910/11/2022Lilian Carpenter2233218111/11/2022Ryan Kaur3182870314/11/2022Annette Williams219397315/11/2022Lilian Carpenter223329016/11/2022Annette Williams2193913717/11/2022Ryan Kaur318286418/11/2022Lilian Carpenter22332179
4Samsung131/10/2022Troy Davis12333413001/11/2022Jean-Luc Chester3245414402/11/2022Dione Hobbs3447413903/11/2022Jean-Luc Chester3245415204/11/2022Dione Hobbs3447486207/11/2022Troy Davis12333417008/11/2022Jean-Luc Chester3245415809/11/2022Dione Hobbs3447414510/11/2022Troy Davis12333411911/11/2022Dione Hobbs3447463314/11/2022Jean-Luc Chester3245410015/11/2022Troy Davis12333411916/11/2022Jean-Luc Chester3245410717/11/2022Dione Hobbs3447417218/11/2022Troy Davis123334121
Results
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Results:
tmpB.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1CompanyWeek NumberDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSales
2Apple110/31/2022Giada Francis102214711/1/2022Nathaniel Figueroa930310711/2/2022John Doe104517011/3/2022Giada Francis102214111/4/2022Nathaniel Figueroa9303113
3Microsoft110/31/2022Lilian Carpenter2233212711/1/2022Ryan Kaur3182812611/2/2022Lilian Carpenter2233211111/3/2022Annette Williams219399411/4/2022Lilian Carpenter22332136
4Samsung110/31/2022Troy Davis12333413011/1/2022Jean-Luc Chester3245414411/2/2022Dione Hobbs3447413911/3/2022Jean-Luc Chester3245415211/4/2022Dione Hobbs3447486
5Apple211/7/2022John Doe104512111/8/2022Nathaniel Figueroa930319211/9/2022John Doe10456211/10/2022Nathaniel Figueroa93036111/11/2022Giada Francis1022146
6Microsoft211/7/2022Annette Williams2193911411/8/2022Ryan Kaur3182819811/9/2022Annette Williams219395911/10/2022Lilian Carpenter2233218111/11/2022Ryan Kaur3182870
7Samsung211/7/2022Troy Davis12333417011/8/2022Jean-Luc Chester3245415811/9/2022Dione Hobbs3447414511/10/2022Troy Davis12333411911/11/2022Dione Hobbs3447463
8Apple311/14/2022John Doe104516211/15/2022Giada Francis10228511/16/2022John Doe10459511/17/2022Giada Francis102216811/18/2022Nathaniel Figueroa9303187
9Microsoft311/14/2022Annette Williams219397311/15/2022Lilian Carpenter223329011/16/2022Annette Williams2193913711/17/2022Ryan Kaur318286411/18/2022Lilian Carpenter22332179
10Samsung311/14/2022Jean-Luc Chester3245410011/15/2022Troy Davis12333411911/16/2022Jean-Luc Chester3245410711/17/2022Dione Hobbs3447417211/18/2022Troy Davis123334121
Results

Code:
VBA Code:
Sub DoSomething()
    Dim WDate As Date
    Dim I As Long, J As Long, Ofs As Long, StaffNumber As Long
    Dim CellRange As Range, Dest As Range, R As Range
    Dim Key As String, LastKey As String, StaffName As String
    Dim Sales As Variant
    Dim WB As Workbook
    Dim WS As Worksheet, WSR As Worksheet

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Raw Data")
    Set WS = WB.Worksheets("Results")
    WS.UsedRange.Clear
    WS.Activate
    Application.ScreenUpdating = False

    With WSR
        Set CellRange = .Range("A1:F" & .Range("B" & .Rows.Count).End(xlUp).Row)
        CellRange.Copy WS.Range("A1")
    End With

    With WS
        Set CellRange = .Range("A2:A" & .Range("B" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
    End With

    For Each R In CellRange
        If IsNumeric(R.Value) And Not Trim(R.Value) = "" Then
            J = R.Value
        End If

        If Trim(R.Value) = "" Then
            R.Value = J
        End If
    Next R

    With WS
        Set CellRange = .Range("C2:C" & .Range("C" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
        LastKey = .Range("C2").Value & "$" & .Range("A2").Value
        Set Dest = .Range("H1")
        Ofs = 1
        I = 1
    End With

    With Dest
        .Value = "Company"
        .Offset(0, 1).Value = "Week Number"
        .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
        .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
    End With

    For Each R In CellRange
        Key = R.Offset(0, 0).Value & "$" & R.Offset(0, -2).Value
        WDate = R.Offset(0, -1).Value
        StaffName = R.Offset(0, 2).Value
        StaffNumber = R.Offset(0, 3).Value
        Sales = R.Offset(0, 1).Value

        If Key <> LastKey Then
            LastKey = Key
            Ofs = Ofs + 1
            I = 1
            With Dest
                .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
                .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
            End With
        End If

        J = WS.Cells(Ofs + 1, WS.Columns.Count).End(xlToLeft).Column
        If I > 1 Then
            J = J + 1
        Else
            I = I + 1
        End If

        WS.Cells(1, J + 1).Value = "Date"
        WS.Cells(Ofs + 1, J + 1).Value = WDate

        WS.Cells(1, J + 2).Value = "Staff Name"
        WS.Cells(Ofs + 1, J + 2).Value = StaffName

        WS.Cells(1, J + 3).Value = "Staff Number"
        WS.Cells(Ofs + 1, J + 3).Value = StaffNumber

        WS.Cells(1, J + 4).Value = "Sales"
        WS.Cells(Ofs + 1, J + 4).Value = Sales
    Next R
    With WS
        .Range("A1:G1").EntireColumn.Delete
        .UsedRange.Columns.ColumnWidth = 5
        .UsedRange.Columns.AutoFit
        .UsedRange.Sort Key1:=.Range("B1"), Order1:=xlAscending, Key2:=.Range("A1"), Order2:=xlAscending, Header:=xlYes
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Results:
tmpB.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1CompanyWeek NumberDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSalesDateStaff NameStaff NumberSales
2Apple110/31/2022Giada Francis102214711/1/2022Nathaniel Figueroa930310711/2/2022John Doe104517011/3/2022Giada Francis102214111/4/2022Nathaniel Figueroa9303113
3Microsoft110/31/2022Lilian Carpenter2233212711/1/2022Ryan Kaur3182812611/2/2022Lilian Carpenter2233211111/3/2022Annette Williams219399411/4/2022Lilian Carpenter22332136
4Samsung110/31/2022Troy Davis12333413011/1/2022Jean-Luc Chester3245414411/2/2022Dione Hobbs3447413911/3/2022Jean-Luc Chester3245415211/4/2022Dione Hobbs3447486
5Apple211/7/2022John Doe104512111/8/2022Nathaniel Figueroa930319211/9/2022John Doe10456211/10/2022Nathaniel Figueroa93036111/11/2022Giada Francis1022146
6Microsoft211/7/2022Annette Williams2193911411/8/2022Ryan Kaur3182819811/9/2022Annette Williams219395911/10/2022Lilian Carpenter2233218111/11/2022Ryan Kaur3182870
7Samsung211/7/2022Troy Davis12333417011/8/2022Jean-Luc Chester3245415811/9/2022Dione Hobbs3447414511/10/2022Troy Davis12333411911/11/2022Dione Hobbs3447463
8Apple311/14/2022John Doe104516211/15/2022Giada Francis10228511/16/2022John Doe10459511/17/2022Giada Francis102216811/18/2022Nathaniel Figueroa9303187
9Microsoft311/14/2022Annette Williams219397311/15/2022Lilian Carpenter223329011/16/2022Annette Williams2193913711/17/2022Ryan Kaur318286411/18/2022Lilian Carpenter22332179
10Samsung311/14/2022Jean-Luc Chester3245410011/15/2022Troy Davis12333411911/16/2022Jean-Luc Chester3245410711/17/2022Dione Hobbs3447417211/18/2022Troy Davis123334121
Results

Code:
VBA Code:
Sub DoSomething()
    Dim WDate As Date
    Dim I As Long, J As Long, Ofs As Long, StaffNumber As Long
    Dim CellRange As Range, Dest As Range, R As Range
    Dim Key As String, LastKey As String, StaffName As String
    Dim Sales As Variant
    Dim WB As Workbook
    Dim WS As Worksheet, WSR As Worksheet

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Raw Data")
    Set WS = WB.Worksheets("Results")
    WS.UsedRange.Clear
    WS.Activate
    Application.ScreenUpdating = False

    With WSR
        Set CellRange = .Range("A1:F" & .Range("B" & .Rows.Count).End(xlUp).Row)
        CellRange.Copy WS.Range("A1")
    End With

    With WS
        Set CellRange = .Range("A2:A" & .Range("B" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
    End With

    For Each R In CellRange
        If IsNumeric(R.Value) And Not Trim(R.Value) = "" Then
            J = R.Value
        End If

        If Trim(R.Value) = "" Then
            R.Value = J
        End If
    Next R

    With WS
        Set CellRange = .Range("C2:C" & .Range("C" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
        LastKey = .Range("C2").Value & "$" & .Range("A2").Value
        Set Dest = .Range("H1")
        Ofs = 1
        I = 1
    End With

    With Dest
        .Value = "Company"
        .Offset(0, 1).Value = "Week Number"
        .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
        .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
    End With

    For Each R In CellRange
        Key = R.Offset(0, 0).Value & "$" & R.Offset(0, -2).Value
        WDate = R.Offset(0, -1).Value
        StaffName = R.Offset(0, 2).Value
        StaffNumber = R.Offset(0, 3).Value
        Sales = R.Offset(0, 1).Value

        If Key <> LastKey Then
            LastKey = Key
            Ofs = Ofs + 1
            I = 1
            With Dest
                .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
                .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
            End With
        End If

        J = WS.Cells(Ofs + 1, WS.Columns.Count).End(xlToLeft).Column
        If I > 1 Then
            J = J + 1
        Else
            I = I + 1
        End If

        WS.Cells(1, J + 1).Value = "Date"
        WS.Cells(Ofs + 1, J + 1).Value = WDate

        WS.Cells(1, J + 2).Value = "Staff Name"
        WS.Cells(Ofs + 1, J + 2).Value = StaffName

        WS.Cells(1, J + 3).Value = "Staff Number"
        WS.Cells(Ofs + 1, J + 3).Value = StaffNumber

        WS.Cells(1, J + 4).Value = "Sales"
        WS.Cells(Ofs + 1, J + 4).Value = Sales
    Next R
    With WS
        .Range("A1:G1").EntireColumn.Delete
        .UsedRange.Columns.ColumnWidth = 5
        .UsedRange.Columns.AutoFit
        .UsedRange.Sort Key1:=.Range("B1"), Order1:=xlAscending, Key2:=.Range("A1"), Order2:=xlAscending, Header:=xlYes
    End With
    Application.ScreenUpdating = True
End Sub

Hello rlv01,

Thank you for the response. I've tried the code you sent which works perfectly, however it doesn't quite give the desired format. Each company needs to be on a single row i.e no duplicates within the company column in sheet "Results"

Here's the desired format for the results:

Excel.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZ
1CompanyWeek NumberDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesWeek NumberDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesWeek NumberDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number SalesDate Staff NameStaff Number Sales
2Apple131/10/2022Giada Francis102214701/11/2022Nathaniel Figueroa930310702/11/2022John Doe104517003/11/2022Giada Francis102214104/11/2022Nathaniel Figueroa9303113207/11/2022John Doe104512108/11/2022Nathaniel Figueroa930319209/11/2022John Doe10456210/11/2022Nathaniel Figueroa93036111/11/2022Giada Francis1022146314/11/2022John Doe104516215/11/2022Giada Francis10228516/11/2022John Doe10459517/11/2022Giada Francis102216818/11/2022Nathaniel Figueroa9303187
3Microsoft131/10/2022Lilian Carpenter2233212701/11/2022Ryan Kaur3182812602/11/2022Lilian Carpenter2233211103/11/2022Annette Williams219399404/11/2022Lilian Carpenter22332136207/11/2022Annette Williams2193911408/11/2022Ryan Kaur3182819809/11/2022Annette Williams219395910/11/2022Lilian Carpenter2233218111/11/2022Ryan Kaur3182870314/11/2022Annette Williams219397315/11/2022Lilian Carpenter223329016/11/2022Annette Williams2193913717/11/2022Ryan Kaur318286418/11/2022Lilian Carpenter22332179
4Samsung131/10/2022Troy Davis12333413001/11/2022Jean-Luc Chester3245414402/11/2022Dione Hobbs3447413903/11/2022Jean-Luc Chester3245415204/11/2022Dione Hobbs3447486207/11/2022Troy Davis12333417008/11/2022Jean-Luc Chester3245415809/11/2022Dione Hobbs3447414510/11/2022Troy Davis12333411911/11/2022Dione Hobbs3447463314/11/2022Jean-Luc Chester3245410015/11/2022Troy Davis12333411916/11/2022Jean-Luc Chester3245410717/11/2022Dione Hobbs3447417218/11/2022Troy Davis123334121
Results


Thank you!
 
Upvote 0
VBA Code:
Sub Reformat()
    Dim WDate As Date
    Dim I As Long, J As Long, Ofs As Long, StaffNumber As Long
    Dim CellRange As Range, Dest As Range, R As Range
    Dim Key As String, LastKey As String, StaffName As String
    Dim Sales As Variant
    Dim WB As Workbook
    Dim WS As Worksheet, WSR As Worksheet
    Dim LastRow As Long
    Dim Company As String, NextCompany As String
    Dim HeaderCol As Long, DataCol As Long
    Dim HeaderColRange As Range, DataColRange As Range

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Raw Data")
    Set WS = WB.Worksheets("Results")
    WS.UsedRange.Clear
    WS.Activate
    Application.ScreenUpdating = False

    With WSR
        Set CellRange = .Range("A1:F" & .Range("B" & .Rows.Count).End(xlUp).Row)
        CellRange.Copy WS.Range("A1")
    End With

    With WS
        Set CellRange = .Range("A2:A" & .Range("B" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
    End With

    For Each R In CellRange
        If IsNumeric(R.Value) And Not Trim(R.Value) = "" Then
            J = R.Value
        End If

        If Trim(R.Value) = "" Then
            R.Value = J
        End If
    Next R

    With WS
        Set CellRange = .Range("C2:C" & .Range("C" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
        LastKey = .Range("C2").Value & "$" & .Range("A2").Value
        Set Dest = .Range("H1")
        Ofs = 1
        I = 1
    End With

    With Dest
        .Value = "Company"
        .Offset(0, 1).Value = "Week Number"
        .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
        .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
    End With

    For Each R In CellRange
        Key = R.Offset(0, 0).Value & "$" & R.Offset(0, -2).Value
        WDate = R.Offset(0, -1).Value
        StaffName = R.Offset(0, 2).Value
        StaffNumber = R.Offset(0, 3).Value
        Sales = R.Offset(0, 1).Value

        If Key <> LastKey Then
            LastKey = Key
            Ofs = Ofs + 1
            I = 1
            With Dest
                .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
                .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
            End With
        End If

        J = WS.Cells(Ofs + 1, WS.Columns.Count).End(xlToLeft).Column
        If I > 1 Then
            J = J + 1
        Else
            I = I + 1
        End If

        WS.Cells(1, J + 1).Value = "Date"
        WS.Cells(Ofs + 1, J + 1).Value = WDate

        WS.Cells(1, J + 2).Value = "Staff Name"
        WS.Cells(Ofs + 1, J + 2).Value = StaffName

        WS.Cells(1, J + 3).Value = "Staff Number"
        WS.Cells(Ofs + 1, J + 3).Value = StaffNumber

        WS.Cells(1, J + 4).Value = "Sales"
        WS.Cells(Ofs + 1, J + 4).Value = Sales
    Next R
    With WS
        .Range("A1:G1").EntireColumn.Delete
    End With

    '2nd part
    With WS
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row    'last row w/data
        Set CellRange = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
    End With

    With WS.Range("B1")
        Set HeaderColRange = WS.Range(.Offset(0, 0), WS.Cells(.Row, WS.Columns.Count).End(xlToLeft))
    End With
    HeaderCol = WS.Cells(1, WS.Columns.Count).End(xlToLeft).Column

    For I = LastRow To 2 Step -1
        DataCol = WS.Cells(I - 1, WS.Columns.Count).End(xlToLeft).Column

        With WS.Range("B1")
            Set DataColRange = WS.Range(.Offset(I - 1, 0), WS.Cells(.Offset(I - 1, 0).Row, WS.Columns.Count).End(xlToLeft))
        End With

        Company = DataColRange.Range("A1").Offset(0, -1).Value
        NextCompany = DataColRange.Range("A1").Offset(-1, -1).Value

        If Company = NextCompany Then
            DataColRange.Cut DataColRange.Offset(-1, DataCol)
            DataCol = WS.Cells(I - 2, WS.Columns.Count).End(xlToLeft).Column
            HeaderColRange.Copy HeaderColRange.Offset(0, DataCol)
            DataColRange.Offset(1).EntireRow.Delete
        End If
    Next I

    WS.UsedRange.Columns.ColumnWidth = 5
    WS.UsedRange.Columns.AutoFit
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
VBA Code:
Sub Reformat()
    Dim WDate As Date
    Dim I As Long, J As Long, Ofs As Long, StaffNumber As Long
    Dim CellRange As Range, Dest As Range, R As Range
    Dim Key As String, LastKey As String, StaffName As String
    Dim Sales As Variant
    Dim WB As Workbook
    Dim WS As Worksheet, WSR As Worksheet
    Dim LastRow As Long
    Dim Company As String, NextCompany As String
    Dim HeaderCol As Long, DataCol As Long
    Dim HeaderColRange As Range, DataColRange As Range

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Raw Data")
    Set WS = WB.Worksheets("Results")
    WS.UsedRange.Clear
    WS.Activate
    Application.ScreenUpdating = False

    With WSR
        Set CellRange = .Range("A1:F" & .Range("B" & .Rows.Count).End(xlUp).Row)
        CellRange.Copy WS.Range("A1")
    End With

    With WS
        Set CellRange = .Range("A2:A" & .Range("B" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
    End With

    For Each R In CellRange
        If IsNumeric(R.Value) And Not Trim(R.Value) = "" Then
            J = R.Value
        End If

        If Trim(R.Value) = "" Then
            R.Value = J
        End If
    Next R

    With WS
        Set CellRange = .Range("C2:C" & .Range("C" & .Rows.Count).End(xlUp).Row)    'range to last cell in column w/data
        LastKey = .Range("C2").Value & "$" & .Range("A2").Value
        Set Dest = .Range("H1")
        Ofs = 1
        I = 1
    End With

    With Dest
        .Value = "Company"
        .Offset(0, 1).Value = "Week Number"
        .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
        .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
    End With

    For Each R In CellRange
        Key = R.Offset(0, 0).Value & "$" & R.Offset(0, -2).Value
        WDate = R.Offset(0, -1).Value
        StaffName = R.Offset(0, 2).Value
        StaffNumber = R.Offset(0, 3).Value
        Sales = R.Offset(0, 1).Value

        If Key <> LastKey Then
            LastKey = Key
            Ofs = Ofs + 1
            I = 1
            With Dest
                .Offset(Ofs, 0).Value = Split(LastKey, "$")(0)
                .Offset(Ofs, 1).Value = Split(LastKey, "$")(1)
            End With
        End If

        J = WS.Cells(Ofs + 1, WS.Columns.Count).End(xlToLeft).Column
        If I > 1 Then
            J = J + 1
        Else
            I = I + 1
        End If

        WS.Cells(1, J + 1).Value = "Date"
        WS.Cells(Ofs + 1, J + 1).Value = WDate

        WS.Cells(1, J + 2).Value = "Staff Name"
        WS.Cells(Ofs + 1, J + 2).Value = StaffName

        WS.Cells(1, J + 3).Value = "Staff Number"
        WS.Cells(Ofs + 1, J + 3).Value = StaffNumber

        WS.Cells(1, J + 4).Value = "Sales"
        WS.Cells(Ofs + 1, J + 4).Value = Sales
    Next R
    With WS
        .Range("A1:G1").EntireColumn.Delete
    End With

    '2nd part
    With WS
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row    'last row w/data
        Set CellRange = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
    End With

    With WS.Range("B1")
        Set HeaderColRange = WS.Range(.Offset(0, 0), WS.Cells(.Row, WS.Columns.Count).End(xlToLeft))
    End With
    HeaderCol = WS.Cells(1, WS.Columns.Count).End(xlToLeft).Column

    For I = LastRow To 2 Step -1
        DataCol = WS.Cells(I - 1, WS.Columns.Count).End(xlToLeft).Column

        With WS.Range("B1")
            Set DataColRange = WS.Range(.Offset(I - 1, 0), WS.Cells(.Offset(I - 1, 0).Row, WS.Columns.Count).End(xlToLeft))
        End With

        Company = DataColRange.Range("A1").Offset(0, -1).Value
        NextCompany = DataColRange.Range("A1").Offset(-1, -1).Value

        If Company = NextCompany Then
            DataColRange.Cut DataColRange.Offset(-1, DataCol)
            DataCol = WS.Cells(I - 2, WS.Columns.Count).End(xlToLeft).Column
            HeaderColRange.Copy HeaderColRange.Offset(0, DataCol)
            DataColRange.Offset(1).EntireRow.Delete
        End If
    Next I

    WS.UsedRange.Columns.ColumnWidth = 5
    WS.UsedRange.Columns.AutoFit
    Application.ScreenUpdating = True
End Sub

Hello rlvo1,

Thank you for the response. The updated code works prefectly and provides the desired format - I appreciate your help.

If you could, could you please some more comments on the code for my understanding for next time as I am relatively new to VBA?

Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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