VBA: Move Named Sheets to New Workbook & Save

NewBlood

New Member
Joined
Sep 17, 2021
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
I found this code in this forum and I was hoping someone could walk me through how I can move sheets that are not named valid, control, and data. The 3 named sheets are my constants and do not need them moved.

VBA Code:
Option Explicit

Sub MoveSheets()
Dim sPath As String
Dim sAddress As String
Dim wbCur As Workbook
Dim wsCur As Worksheet

'-- Store path of this workbook --
sPath = ThisWorkbook.Path & Application.PathSeparator

'-- Loop thru worksheets --
For Each wsCur In ThisWorkbook.Worksheets
    On Error Resume Next
    Set wbCur = Nothing
    '-- Add a new workbook --
    Set wbCur = Workbooks.Add
    On Error GoTo 0
    If wbCur Is Nothing Then
        '-- Report any error --
        MsgBox prompt:=Err.Description
    Else
        '-- Rename sheet 1 of new workbook --
        wbCur.Sheets(1).Name = wsCur.Name
        '-- Get range address of input data --
        sAddress = wsCur.UsedRange.Address
        '-- Copy & paste data --
        wsCur.UsedRange.Copy Destination:=wbCur.Sheets(1).Range(sAddress)
        '-- Save new workbook with filename = current worksheet name --
        wbCur.Close savechanges:=True, Filename:=sPath & wsCur.Name & ".xlsx"
    End If
Next wsCur
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Not tested.

VBA Code:
Sub MoveSheets()
    Dim sPath As String
    Dim sAddress As String
    Dim wbCur As Workbook
    Dim wsCur As Worksheet
    
    '-- Store path of this workbook --
    sPath = ThisWorkbook.Path & Application.PathSeparator
    
    '-- Loop thru worksheets --
    For Each wsCur In ThisWorkbook.Worksheets
        Select Case wsCur.Name
            Case "valid", "control", "data" 'worksheet exclude list
            Case Else
                On Error Resume Next
                Set wbCur = Nothing
                
                '-- Add a new workbook --
                Set wbCur = Workbooks.Add
                On Error GoTo 0
                
                If wbCur Is Nothing Then
                    '-- Report any error --
                    MsgBox prompt:=Err.Description
                Else
                    '-- Rename sheet 1 of new workbook --
                    wbCur.Sheets(1).Name = wsCur.Name
                    
                    '-- Get range address of input data --
                    sAddress = wsCur.UsedRange.Address
                    
                    '-- Copy & paste data --
                    wsCur.UsedRange.Copy Destination:=wbCur.Sheets(1).Range(sAddress)
                    
                    '-- Save new workbook with filename = current worksheet name --
                    wbCur.Close savechanges:=True, Filename:=sPath & wsCur.Name & ".xlsx"
                End If
        End Select
    Next wsCur
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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