VBA how to prevent workbook from creating "Book1 - Excel"

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Good Day All,

I'm using the sub SaveAsXLSM below to create a new workbook based upon the date.

VBA Code:
    Dim strName As String
    
    strName = ThisWorkbook.Path & "\" & Format(Date, "DDMMMYYYY ") & Sheet1.Name & " " & ".xlsm"
    
    ActiveSheet.Copy
    ThisWorkbook.SaveAs FileName:=strName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    ActiveWorkbook.Close savechanges:=False

At times depending upon the computer speed provides a "Book1 - Excel" file as an error file.

My question is, how do prevent this type of event/error from happening?

For example,

VBA Code:
 On Error GoTo Whoa

'code

LetsContinue:

  If strWBName = "Book1.xlsx" created Then

 Don't 

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
    
Whoa:
    'MsgBox Err.Description
    Resume LetsContinue


Please let me know.

Thank you!
pinaceous
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
try putting a pause between activesheet.copy and thisworkbook,saveas


eg:
Code:
ActiveSheet.Copy
Call pausemacro(3)
ThisWorkbook.SaveAs FileName:=strName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
ActiveWorkbook.Close savechanges:=False

Code:
Sub pausemacro(delaysec)
'pause4 (delaysec) secs
  newHour = Hour(Now()): newMinute = Minute(Now()): newSecond = Second(Now()) + delaysec
  waitTime = TimeSerial(newHour, newMinute, newSecond): Application.Wait waitTime
End Sub
waitTime = TimeSerial(newHour, newMinute, newSecond): Application.Wait waitTime
End Sub
 
Upvote 0
I don't understand your code. It says to copy the activesheet - which creates a new, active workbook - then save the workbook with the code in it, then close the workbook you just created without saving it. What was the point?
 
Upvote 0
Not sure where Post#2 was going but this is the solution that I came up with:

VBA Code:
Sub CloseOtherWorkbook()
Dim xWB As Workbook
Application.ScreenUpdating = False
For Each xWB In Application.Workbooks
    If Not (xWB Is Application.ActiveWorkbook) Then
        xWB.Close
    End If
Next
Application.ScreenUpdating = True
End Sub

Thank you for posting!
pinaceous
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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