Loop through worksheets and copy to workbook

NichoD

Board Regular
Joined
Jul 31, 2022
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello, I have a worksheet where everyday a new worksheet is created with todays date formated as YYMMDD. At open workbook, I would like vba to loop through all worksheets and copy worksheets with YYMMDD - 1 and onwards to another workbook, and thereafter be deleated from the current workbook. Could anyone help me with this?

Thank you in advance.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I have assumed that the workbook running the code contains the worksheets that you want to move. If you want to refer to the active workbook, replace...

VBA Code:
Set sourceWorkbook = ThisWorkbook

with

VBA Code:
Set sourceWorkbook = ActiveWorkbook

I have also assumed that you want to move those worksheets to a new workbook. If you want to move them to an already opened workbook, replace...

VBA Code:
Set destinationWorkbook = Workbooks.Add(xlWBATWorksheet)

with

VBA Code:
Set destinationWorkbook = Workbooks("Book2.xlsx") 'change the workbook name accordingly

Here's the macro...

VBA Code:
Option Explicit

Sub MoveWorkSheetsToOtherWorkbook()

    Dim sourceWorkbook As Workbook
    Set sourceWorkbook = ThisWorkbook
   
    Dim destinationWorkbook As Workbook
    Set destinationWorkbook = Workbooks.Add(xlWBATWorksheet)
   
    Dim totalSheetCount As Long
    totalSheetCount = sourceWorkbook.Sheets.Count
   
    Dim sheetCount As Long
    sheetCount = 0
   
    Dim currentWorksheet As Worksheet
    For Each currentWorksheet In sourceWorkbook.Worksheets
        If currentWorksheet.Name Like "###### - 1" Then
            sheetCount = sheetCount + 1
            If totalSheetCount - sheetCount = 0 Then
                MsgBox "Unable to move " & currentWorksheet.Name & "!", vbExclamation
                Exit Sub
            End If
            currentWorksheet.Move after:=destinationWorkbook.Sheets(destinationWorkbook.Sheets.Count)
        End If
    Next currentWorksheet
   
    MsgBox "Number of worksheets moved: " & sheetCount, vbInformation
   
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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