How to convert range with multiple columns into 3 columns range

Sinuhet

New Member
Joined
Feb 9, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
My apologies, but I need help
I would like convert a multicolumn range (schedule)
AlanCharlesJamesSusanBetty
11/14/2022short daylong daynight
11/15/2022nightshort daylong day
11/16/2022nightshort daylong day

into range with just 3 columns:
DateShift nameEmployee
11/14/2022short dayCharles
11/14/2022long daySusan
11/14/2022nightBetty
11/15/2022nightAlan
11/15/2022short dayJames
11/15/2022long dayBetty
11/16/2022nightCharles
11/16/2022short dayJames
11/16/2022long daySusan

Days with no shift assigned to a person should not generate an extra row. The generated rows do not have to be necessary in chronological way.
Thank you very much for any help
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this macro. The result will be placed in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub ConvertRange2()
    Application.ScreenUpdating = False
    Dim rng As Range, lCol As Long, srcWS As Worksheet, desWS As Worksheet, x As Long, cnt As Long, lRow As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    With desWS
        .UsedRange.ClearContents
        .Range("A1").Resize(, 3) = Array("Date", "Shift Name", "Employee")
    End With
    With srcWS
        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For x = 2 To lRow
            cnt = WorksheetFunction.CountA(.Rows(x)) - 1
            With desWS
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(cnt) = srcWS.Range("A" & x)
                For Each rng In srcWS.Range("B" & x).Resize(, lCol - 1)
                    If rng <> "" Then
                        .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 2) = Array(rng, srcWS.Cells(1, rng.Column))
                    End If
                Next rng
            End With
        Next x
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try this macro. The result will be placed in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub ConvertRange2()
    Application.ScreenUpdating = False
    Dim rng As Range, lCol As Long, srcWS As Worksheet, desWS As Worksheet, x As Long, cnt As Long, lRow As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    With desWS
        .UsedRange.ClearContents
        .Range("A1").Resize(, 3) = Array("Date", "Shift Name", "Employee")
    End With
    With srcWS
        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For x = 2 To lRow
            cnt = WorksheetFunction.CountA(.Rows(x)) - 1
            With desWS
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(cnt) = srcWS.Range("A" & x)
                For Each rng In srcWS.Range("B" & x).Resize(, lCol - 1)
                    If rng <> "" Then
                        .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 2) = Array(rng, srcWS.Cells(1, rng.Column))
                    End If
                Next rng
            End With
        Next x
    End With
    Application.ScreenUpdating = True
End Sub
it works just fine! Thank you so much
 
Upvote 0

Forum statistics

Threads
1,215,603
Messages
6,125,776
Members
449,259
Latest member
rehanahmadawan

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