I still haven't found out why my macro will perform my On Error routine the first time an error occurs but not the second time.
I am having a macro save a workbook with a name that includes a date from one of the sheets.
If the File name already exists, the user gets the usual prompt: File already exists, replace?
If they say No, my On Error routine works and gives them a textbox to enter in a new name. Then I do a GoTo SaveFile to try and save the new file name.
If the name they enter already exists, an error occurs but is not trapped for some reason with the On Error code, even though it is the same error that hit the trap the first time. Do I need to do something to re-set this when I call the same section again? See code below:
Application.DisplayAlerts = True
Sheets("Master Data").Select
FileMM = Cells(1, 4).Value
FileDD = Cells(1, 5).Value
FileYY = Cells(1, 6).Value
Dim sFilename As String
sFilename = "G:\Payroll\Manual Batch Sheet Macros\Manual Batch Sheet Test " & FileMM & "-" & FileDD & "-" & FileYY & ".xls"
SaveSheet:
On Error GoTo Newname
ActiveWorkbook.SaveAs Filename:=sFilename, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Exit Sub
Newname:
NewFilename = Application.InputBox("Please enter a new name for this file. Example: Manual Batch Sheet 01-01-06")
If NewFilename = False Or NewFilename = "" Then
ActiveWorkbook.Close False
Exit Sub
Else
sFilename = "G:\Payroll\Manual Batch Sheet Macros\" & NewFilename & ".xls"
GoTo SaveSheet
End If
End Sub
I am having a macro save a workbook with a name that includes a date from one of the sheets.
If the File name already exists, the user gets the usual prompt: File already exists, replace?
If they say No, my On Error routine works and gives them a textbox to enter in a new name. Then I do a GoTo SaveFile to try and save the new file name.
If the name they enter already exists, an error occurs but is not trapped for some reason with the On Error code, even though it is the same error that hit the trap the first time. Do I need to do something to re-set this when I call the same section again? See code below:
Application.DisplayAlerts = True
Sheets("Master Data").Select
FileMM = Cells(1, 4).Value
FileDD = Cells(1, 5).Value
FileYY = Cells(1, 6).Value
Dim sFilename As String
sFilename = "G:\Payroll\Manual Batch Sheet Macros\Manual Batch Sheet Test " & FileMM & "-" & FileDD & "-" & FileYY & ".xls"
SaveSheet:
On Error GoTo Newname
ActiveWorkbook.SaveAs Filename:=sFilename, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Exit Sub
Newname:
NewFilename = Application.InputBox("Please enter a new name for this file. Example: Manual Batch Sheet 01-01-06")
If NewFilename = False Or NewFilename = "" Then
ActiveWorkbook.Close False
Exit Sub
Else
sFilename = "G:\Payroll\Manual Batch Sheet Macros\" & NewFilename & ".xls"
GoTo SaveSheet
End If
End Sub