VB Export tabs to specific folders

jwmi01

New Member
Joined
Jul 20, 2023
Messages
9
Office Version
  1. 2021
Platform
  1. Windows
Following script exports specific tabs in the workbook to one folder (Manual Backups) on a network. Looking for a way to export each tab to its own specific folder.

i.e. worksheet "Apples" in the array saves to folder "Apples" etc. Any suggestions are appreciated!

VBA Code:
Sub ExportWorksheetsToSpecificFolderTest()

    Dim worksheet_list As Variant, worksheet_name As Variant
    Dim new_workbook As Workbook
    Dim saved_folder As String
    
    worksheet_list = Array("Apples", "Oranges", "Bananas", "Grapes")
    '// close the path with a back slash
    saved_folder = "\\ABC-DC01\ABCComm\Common\District Data\Daily Report\Manual Backups\"
    
    For Each worksheet_name In worksheet_list
    
        On Error Resume Next
       
        Set new_workbook = Workbooks.Add
        
        ThisWorkbook.Worksheets(worksheet_name).Copy new_workbook.Worksheets(1)
        
        new_workbook.SaveAs saved_folder & worksheet_name & " " & Format(Now(), "MM-DD-YY") & ".csv", 6
        new_workbook.Close False
        
    Next worksheet_name
    
    MsgBox "Export complete.", vbInformation
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I did not test this, but I think if you move the saved_folder = ... line inside your loop you will have a solution.

This code should
  1. test for the existence of the backup folder for the Worksheet (path & folder_name)
  2. if it does not exist, create it
  3. save the new workbook to the folder from 1. or 2. above

Please try testing this (I gave it a quick test, but not for a network path) and let me know what happens. .

I couldn't determine what the worksheet_list array was for. It does not appear to be used.

VBA Code:
Sub ExportWorksheetsToSpecificFolderTest()

    Dim worksheet_list As Variant, worksheet_name As Variant
    Dim new_workbook As Workbook
    Dim saved_path As String
    Dim saved_folder As String
    Dim fs 
    
    worksheet_list = Array("Apples", "Oranges", "Bananas", "Grapes")
    '// close the path with a back slash
    saved_path= "\\ABC-DC01\ABCComm\Common\District Data\Daily Report\Manual Backups\"
    
    For Each worksheet_name In worksheet_list

      saved_folder = saved_path & worksheet_name
      Set fs = CreateObject("Scripting.FileSystemObject")
      If fs.folderexists(saved_folder ) = False Then
          fs.createfolder saved_folder
      End If

    
        On Error Resume Next
       
        Set new_workbook = Workbooks.Add
        
        ThisWorkbook.Worksheets(worksheet_name).Copy new_workbook.Worksheets(1)
        
        new_workbook.SaveAs saved_folder & worksheet_name & " " & Format(Now(), "MM-DD-YY") & ".csv", 6
        new_workbook.Close False
        
    Next worksheet_name
    
    MsgBox "Export complete.", vbInformation
End Sub
 
Upvote 0
please change the line of code that defines "save_folder" (inside your loop) to:

saved_folder = saved_path & worksheet_name & "\"
 
Upvote 0
Appreciate you taking a look.

Getting a 76 path not found error here

1690944796726.png
 
Upvote 0
You might put a msgbox in where above where the error occurs to verify the value of saved folder.
 
Upvote 0
Try this modification … and let me know.

Remove the & “\” that you added

And modify the .SaveAs line to
new_workbook.SaveAs saved_folder & “\” & worksheet_name & " " & Format(Now(), "MM-DD-YY") & ".csv", 6
 
Upvote 0
Runs with no error and gives Export Complete msg but there's no output. changed path to a local folder with same result.

Not sure if this helps, but the Array is designating the specific sheets in the workbook to export. It has 10, but I only need 4 of those 10. I should have clarified that.
 
Upvote 0
this code worked on my NAS drive ...

VBA Code:
Sub ExportWorksheetsToSpecificFolderTest()

    Dim worksheet_list As Variant, worksheet_name As Variant
    Dim new_workbook As Workbook
    Dim saved_path As String
    Dim saved_folder As String
    Dim fs
   
    worksheet_list = Array("Apples", "Oranges", "Bananas", "Grapes")
    '// close the path with a back slash
    saved_path = "\\ABC-DC01\ABCComm\Common\District Data\Daily Report\Manual Backups\"
   
    For Each worksheet_name In worksheet_list

      saved_folder = saved_path & worksheet_name
      Set fs = CreateObject("Scripting.FileSystemObject")
      If fs.folderexists(saved_folder) = False Then
          fs.createfolder saved_folder
      End If

   
        On Error Resume Next
      
        Set new_workbook = Workbooks.Add
       
        ThisWorkbook.Worksheets(worksheet_name).Copy new_workbook.Worksheets(1)
       
        new_workbook.SaveAs saved_folder & "\" & worksheet_name & " " & Format(Now(), "MM-DD-YY") & ".csv", 6
        new_workbook.Close False
       
    Next worksheet_name
   
    MsgBox "Export complete.", vbInformation
End Sub
 
Upvote 0
Solution
You are 100% correct.....I wasn't adding the saved folder path under the "For Each worksheet_name" so it was saving to My Documents ;)

Thanks so much for the assistance, I greatly appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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