Hi all, long time no see. I've been working foreign and exotic places.
Here is the macro:
Sub Save_Report()
Dim RepName as string 'Name for the saved workbook
Repname = Sheets("Report").Range("A1").Value
Dim WBpath As String 'Select current workbook path, so saves in same folder
WBpath = ActiveWorkbook.Path
Application.DisplayAlerts = False 'so overwrites if already there
ActiveSheet.Copy 'copies current sheet
On Error Resume Next 'if no book1, then defer to book2, etc...
Windows("Book1").Activate '<<< ERROR here if no book1
Windows("Book2").Activate
Windows("Book3").Activate
Windows("Book4").Activate
Windows("Book5").Activate
Windows("Book6").Activate
Windows("Book7").Activate
Windows("Book8").Activate
Windows("Book9").Activate
On Error GoTo 0
ChDir WBpath
RepName = Application.SaveAsFilename(RepName, _
fileFilter:="Microsoft Excel Workbook (*.xls), *.xls")
ActiveWorkbook.SaveAs RepName
ActiveWorkbook.Close
End Sub
Display More
Now the problem is, in one workbook this works fine if this is the second to ninth save, but in a different workbook (the one I am currently working on), it does not resume next and defaults to an error. Is there any way to see if exists a window with the ("book#") name 1 through 9, and if so, then select it?
Thanks in advance![hr]*[/hr] Auto Merged Post;[dl]*[/dl]Hi,
I figured out the answer. See below code:
Sub Save_Report()
Dim RepName As String 'Name for the saved workbook
Repname = Sheets("Report").Range("A1").Value
Dim WBpath As String 'Select current workbook path, so saves in same folder
WBpath = ActiveWorkbook.Path
Application.DisplayAlerts = False 'so overwrites if already there
ActiveSheet.Copy 'copies current sheet
Dim myWb As String ' <<< Fixed with this code
myWb = ActiveWorkbook.Name
Windows(myWb).Activate
ChDir WBpath
RepName = Application.SaveAsFilename(RepName, _
fileFilter:="Microsoft Excel Workbook (*.xls), *.xls")
ActiveWorkbook.SaveAs RepName
ActiveWorkbook.Close
End Sub
Display More
Thanks for browsing 