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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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,215,443
Messages
6,124,890
Members
449,194
Latest member
JayEggleton

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