Only run macro if there is no errors previously

cssfonseca

New Member
Joined
Aug 14, 2018
Messages
19
Hi. I writing a macro to save my file and to do some procedures after it, but I ran into a little error. When I run the macro, and it asks for the file location and I escape, the macro goes to the error but it stills cleans the sheets (limpafolhas), which I don't want to, unless the saving is successful.

Basically, I want when is no error, to go to that. Can u help me out? When I'm searching for this topic I've always ended up in an On Error GoTo 0, but I think that's not what I need.

And, just a small question, to save without any macros, the file type is correct?

Code:
Sub salvar()Dim fPath As Variant


fPath = _
    Application.GetSaveAsFilename(InitialFileName:=Left(ThisWorkbook.Name, _
    InStr(1, ThisWorkbook.Name, ".xls") - 1), _
    filefilter:="Excel Files (*.xlsx*), *.xlsx*")
    
    'deletes all sheets except 1
    limpafolhas
    'unhides all rows
    UnhideAll
    
On Error Resume Next
If fPath = False Then
    MsgBox "File path not found - try again."
    Exit Sub
End If
On Error GoTo 0
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CStr(fPath) & "xls", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi,

You could test following tiny change

Code:
Sub Salvar2()
Dim fPath As Variant
fPath = _
    Application.GetSaveAsFilename(InitialFileName:=Left(ThisWorkbook.Name, _
    InStr(1, ThisWorkbook.Name, ".xls") - 1), _
    filefilter:="Excel Files (*.xlsx*), *.xlsx*")


If fPath = False Then
    MsgBox "File path not found - try again."
    Exit Sub
End If


'deletes all sheets except 1
    limpafolhas
    'unhides all rows
    UnhideAll


Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CStr(fPath) & "xls", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub

HTH
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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