Extract columns and copy to separate worksheet

AndyMcC

New Member
Joined
Nov 27, 2003
Messages
21
I wish to copy the data from this table into seven separate worksheets. The numbers in Row 1 represent days of the week using the Weekday() function, and the seven worksheets contain data for each day of the week; the tabs at the bottom. In other words I want to extract all the data from columns with a 1 in Row 1 and paste them into the worksheet entitled Sunday, and so on. This is the XL2BB extract for the selected area in the screenshot.
I'm sure this is very simple but my VBA is a little rusty now that I'm retired.
Thanks


2023 Weeks 5 to 8.xlsm
ABCDEFGHIJK
11234567123
2 Start time29/01/2330/01/2331/01/2301/02/2302/02/2303/02/2304/02/2305/02/2306/02/2307/02/23
300:000.3050.390.320.5170.1210.080.120.1470.3070.233
400:300.3260.0770.450.0760.4490.1150.1260.3490.3220.194
501:000.0890.5570.0960.5230.1630.3650.0750.1530.0890.385
601:300.5390.080.5270.0890.3170.2190.0960.110.6720.186
702:000.090.3380.1250.3890.2170.1570.2490.1020.0680.633
802:300.4280.2540.5370.2330.2210.1240.2820.530.3130.143
903:000.3830.1050.2380.3170.3540.470.2690.1840.2870.5
1003:300.1010.5050.540.3130.1190.0790.0910.0750.0710.244
1104:000.5220.0880.1280.1730.4690.090.3460.4560.5210.402
1204:300.0730.1820.4960.4520.0810.4870.1860.1250.1290.368
1305:000.3110.4460.1320.1310.4880.0770.1020.1640.4270.354
1405:300.350.070.2590.5410.090.0840.1190.3950.2930.526
1506:000.280.3920.4220.0890.2520.5120.5310.1080.2520.306
1606:300.6430.280.4910.6510.3020.0920.3250.750.3930.552
1707:000.4980.530.6680.3540.150.6840.5120.2880.4710.464
1807:300.4130.4880.2050.7340.4670.1730.3350.6580.5090.652
1908:000.4710.180.5130.0670.1570.3920.1150.1070.1070.293
2008:300.2750.4050.0350.0670.3220.1890.5450.3470.1840.401
2109:000.350.0260.080.0570.3460.0070.0620.0360.0330.243
2209:300.0730.1460.1410.0410.2070.1240.1940.0020.0060.436
2310:000.1680.0030.2760.0160.1980.0260.2430.0330.0030.297
2410:300.6580.0480.3210.3890.2060.4950.730.1010.1050.02
2511:000.0130.0050.0170.0280.3540.0070.0110.0090.0070.87
2611:300.5010.0030.8440.0070.2050.0030.0260.3240.0070.3
2712:001.8860.0031.8790.0030.0650.0040.0062.1960.0050.005
2812:300.9180.0040.3050.0040.0790.0040.0041.7270.0040.003
Weeks 5 to 8
Cell Formulas
RangeFormula
B1:K1B1=WEEKDAY(B2)
C2:K2C2=B2+1
 

Attachments

  • 2023-02-20.jpg
    2023-02-20.jpg
    54.1 KB · Views: 10

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
One way.
VBA Code:
Sub DoSomething()
    Dim WS As Worksheet
    Dim rngColData As Range, DestRange As Range
    Dim rngRowData As Range
    Dim R As Range
    Dim DayName As String
    
    Set WS = ActiveSheet
    With WS
        Set rngRowData = .Range("B1", .Cells(1, .Columns.Count).End(xlToLeft))
        
        For Each R In rngRowData
            Select Case R.Value
                Case 1
                    DayName = "Sunday"
                Case 2
                    DayName = "Monday"
                Case 3
                    DayName = "Tuesday"
                Case 4
                    DayName = "Wednesday"
                Case 5
                    DayName = "Thursday"
                Case 6
                    DayName = "Friday"
                Case 7
                    DayName = "Saturday"
                Case Else
                    DayName = ""
            End Select
            
            If DayName <> "" Then
                Set rngColData = .Range(.Cells(2, R.Column), .Cells(.Rows.Count, R.Column).End(xlUp))
                With Worksheets(DayName)
                    If .UsedRange.Cells.Count > 1 Then
                        Set DestRange = .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1)
                    Else
                        Set DestRange = .Range("A1")
                    End If
                End With
                If Not (rngColData Is Nothing Or DestRange Is Nothing) Then
                    rngColData.Copy
                    DestRange.PasteSpecial xlPasteValuesAndNumberFormats
                    DestRange.Columns.AutoFit
                End If
                Set rngColData = Nothing
                Set DestRange = Nothing
            End If
        Next R
    End With
End Sub
 
Upvote 0
Try this macro, which also copies the Start time column to column A in the destination sheets.
VBA Code:
Public Sub Copy_Columns_To_Weekday_Sheets()

    Dim c As Long
    Dim destCell As Range
    
    With Worksheets("Weeks 5 to 8")
        For c = 2 To .Cells(1, .Columns.Count).End(xlToLeft).Column
            With Worksheets(Format(.Cells(2, c).Value, "dddd"))
                Set destCell = .Cells(1, .Columns.Count).End(xlToLeft)
                If Not IsEmpty(destCell.Value) Then Set destCell = destCell.Offset(, 1)
            End With
            If destCell.Column = 1 Then
                'Copy Start time column
                .Columns(1).Copy
                destCell.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                destCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Set destCell = destCell.Offset(, 1)
            End If
            'Copy date column
            .Columns(c).Copy
            destCell.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            destCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Next
    End With
   
    Application.CutCopyMode = False
   
End Sub
 
Upvote 0
One way.
VBA Code:
Sub DoSomething()
    Dim WS As Worksheet
    Dim rngColData As Range, DestRange As Range
    Dim rngRowData As Range
    Dim R As Range
    Dim DayName As String
   
    Set WS = ActiveSheet
    With WS
        Set rngRowData = .Range("B1", .Cells(1, .Columns.Count).End(xlToLeft))
       
        For Each R In rngRowData
            Select Case R.Value
                Case 1
                    DayName = "Sunday"
                Case 2
                    DayName = "Monday"
                Case 3
                    DayName = "Tuesday"
                Case 4
                    DayName = "Wednesday"
                Case 5
                    DayName = "Thursday"
                Case 6
                    DayName = "Friday"
                Case 7
                    DayName = "Saturday"
                Case Else
                    DayName = ""
            End Select
           
            If DayName <> "" Then
                Set rngColData = .Range(.Cells(2, R.Column), .Cells(.Rows.Count, R.Column).End(xlUp))
                With Worksheets(DayName)
                    If .UsedRange.Cells.Count > 1 Then
                        Set DestRange = .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1)
                    Else
                        Set DestRange = .Range("A1")
                    End If
                End With
                If Not (rngColData Is Nothing Or DestRange Is Nothing) Then
                    rngColData.Copy
                    DestRange.PasteSpecial xlPasteValuesAndNumberFormats
                    DestRange.Columns.AutoFit
                End If
                Set rngColData = Nothing
                Set DestRange = Nothing
            End If
        Next R
    End With
End Sub
Brilliant. Thanks, that works perfectly
 
Upvote 0
rlv01. Many thanks . That is brilliant. One small refinement if I could trouble you again. Before the macro starts the sorting and copying it needs to go to each of the "Day" pages and delete any previous data in columns B to E. That'll enable me to be able to rename the workbook and use it in the future.
 
Upvote 0
Why aren't you deleting column A as well?
 
Upvote 0
Then perhaps something like this
VBA Code:
Sub ClearData()
    Dim WB As Workbook
    Dim WS As Worksheet

    Set WB = ThisWorkbook
    Set WS = ActiveSheet

    For Each WS In WB.Worksheets
        Select Case WS.Name
        Case "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
         WS.UsedRange.Offset(0, 1).Cells.Clear
        End Select
   Next WS
End Sub
 
Upvote 0
Then perhaps something like this
VBA Code:
Sub ClearData()
    Dim WB As Workbook
    Dim WS As Worksheet

    Set WB = ThisWorkbook
    Set WS = ActiveSheet

    For Each WS In WB.Worksheets
        Select Case WS.Name
        Case "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
         WS.UsedRange.Offset(0, 1).Cells.Clear
        End Select
   Next WS
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,256
Members
449,149
Latest member
mwdbActuary

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