Save Multiple Active Workbooks

temerson

New Member
Joined
Apr 22, 2019
Messages
39
Hello,

I have the following code:

Sub VisibleSheets(Optional ByVal Control As IRibbonControl)
On Error Resume Next


Dim ws As Worksheet
Dim wbNew As Workbook
Dim WSHShell As Object
Dim DesktopPath As String
Dim VendorName As String
Dim StoreName As String


VendorName = Range("I2")
StoreName = Range("J2")


Set WSHShell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")

For Each ws In ActiveWorkbook.Sheets
If ws.Visible Then
Debug.Print "Exporting: " & ws.Name
ws.Copy
Set wbNew = Application.ActiveWorkbook

Set wbNew = Nothing


DesktopPath = WSHShell.SpecialFolders("Desktop")
ActiveWorkbook.SaveAs DesktopPath & "\#" & StoreName & " OPENING DAIRY ORDER-" & VendorName & ".xls"


End If
Next ws

Set ws = Nothing
Set WSHShell = Nothing


End Sub

The end goal is to have each tab in the workbook have its own workbook, then save it onto my desktop. The problem I am facing is after the first workbook is saved, the proceeding active workbooks fail to save.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Change your code to the following macro.

Code:
Sub Save_Multiple_Workbooks()
  Dim ws As Worksheet, wbNew As Workbook, DPath As String, VendorN As String, StoreN As String
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  DPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
  For Each ws In ThisWorkbook.Sheets
    If ws.Visible Then
      VendorN = ws.Range("I2")
      StoreN = ws.Range("J2")
      ws.Copy
      Set wbNew = ActiveWorkbook
      wbNew.SaveAs DPath & "\#" & StoreN & " OPENING DAIRY ORDER-" & VendorN & ".xls"
      wbNew.Close False
    End If
  Next ws
  Set ws = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,971
Members
452,540
Latest member
haasro02

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