Move Sheets Containing... ???

DouglasWicker

New Member
Joined
Aug 8, 2017
Messages
38
Morning All,

I have a workbook that consists of 1000+ sheets. They all have a weekday and a duplicate number as their sheet names. e.g Monday 12, Tuesday 15, Sunday 34 etc. THey are not in order of days. I'm trying to use vba to move all sheets containing "Monday" into a new workbook so will then Only have Mondays in that workbook.

I currently have this code but am getting debug errors on the ActiveSheet.Move line.
Code:
Sub MoveMondays()
Dim ws As Worksheet


    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "Monday*" Then
            ActiveSheet.Move Before:=Workbooks("Monday Test.xlsx").Sheets(1)
        End If
    
    Next ws
    
End Sub
 
Last edited by a moderator:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try changing ActiveSheet.Move to ws.Move
If that doesn't work, what does the error say?
 
Last edited:
Upvote 0
Try:
Code:
Sub MoveMondays()

    Dim ws      As Worksheet
    Dim wkb     As Workbook
    Dim strDest As String

    strDest = "Monday Test.xlsx"

    On Error GoTo ExitMe
        Set wkb = Workbooks(strDest)
    On Error GoTo 0
    
    If ActiveWorkbook.Worksheets.count = 1 Then
        MsgBox "Not enough worksheets to move", vbExclamation, "Invalid File"
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    For Each ws In ActiveWorkbook.Worksheets
        If InStr(ws.Name, "Monday") Then ws.Move before:=wkb.Sheets(1)
        If ActiveWorkbook.Worksheets.count < 2 Then Exit For
    Next ws
    
    Application.ScreenUpdating = True
    Set wkb = Nothing
    Exit Sub
    
ExitMe:
    MsgBox Replace("Workbooks: @wkb does not exist", "@wkb", strDest), vbExclamation, "Workbook not found"
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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