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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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