Save a specific sheet in a new workbook

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello. I have a workbook that contains several sheets. I would like to copy Sheets(main) sheet into a separate workbook and save it inside a folder that is automatically created if it does not exist, named (Report 2024) in the same path as the main workbook.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
try this:
VBA Code:
Sub copyMain()
    Const shtName As String = "Sheets(main)"
    Const subFolderName As String = "Report 2024"
    Const newFileName As String = "reportFile"
   
    Dim newWb As Workbook, wb As Workbook
    Dim idx As Long, fPath As String
   
    Set wb = ThisWorkbook
    idx = Application.Workbooks.Count
    wb.Worksheets(shtName).Copy
    Set newWb = Application.Workbooks(idx + 1)
    fPath = TrailingSlash(TrailingSlash(wb.Path) & subFolderName)
    If FolderExists(fPath, True) Then
        newWb.SaveAs fPath & newFileName, xlWorkbookDefault
    End If
    Set newWb = Nothing
    Set wb = Nothing
End Sub

Public Function TrailingSlash(varIn As Variant) As String
    'The TrailingSlash() function just ensures that _
    the folder names we are processing end with the slash character.

    If Len(varIn) > 0& Then
        If Right$(varIn, 1&) = Application.PathSeparator _
                Then TrailingSlash = varIn _
                Else TrailingSlash = varIn & Application.PathSeparator
    End If

End Function

Function FolderExists(ByVal strFolderName As String, Optional ForceMakeDir As Boolean = False) As Boolean
    Dim fso As New FileSystemObject
    FolderExists = fso.FolderExists(strFolderName)
       
    On Error Resume Next
    If ForceMakeDir And (Not FolderExists) Then
        fso.CreateFolder (strFolderName)
        FolderExists = fso.FolderExists(strFolderName)
       
        If Not FolderExists Then
            Dim i As Long, x As String, y
            If strFolderName Like "?:\*" Then
                x = Left(strFolderName, 3)
                y = Split(Mid(strFolderName, 4), Application.PathSeparator)
            ElseIf strFolderName Like "\\*" Then
                x = Left(strFolderName, 2)
                y = Split(Mid(strFolderName, 3), Application.PathSeparator)
            Else
                x = ""
                y = Null
            End If
       
            If (x <> "") And IsArray(y) Then
                For i = LBound(y) To UBound(y)
                    x = TrailingSlash(x) & y(i)
                    If Not FolderExists(x, True) Then Exit For
                Next i
            End If
            FolderExists = fso.FolderExists(strFolderName) 'Dir(strFolderName, vbDirectory) <> ""
        End If
       
        y = Null
    End If
    Set fso = Nothing
   
End Function
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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