Error in Macrr when user hit cancel or X button

Dedeke

Board Regular
Joined
Dec 1, 2020
Messages
70
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I'm using the code below to save sheets with colored tab as pdf to a location the can choose themself.
The code is activated by a CmdBtn
So when button clicked, the standard MSO "save a"s screen opens ( see pictures also please)
When users hit cancel or X button, i get an error.
Logical i think while user interrupts the code.

Is it possible to get a message instead( something like " aborted by user" ) and at least the last part of the code is executed( hiding the colored TABS again.

Many thx already

VBA Code:
Public Sub TabDuoCreate_PDFs()

'Maakt van alle duo een pdf van het overzicht

 Application.ScreenUpdating = False
 Call Unhide_Colored_Tabs36Duo

Dim Folder_Path As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Selecteer de gewenste folder"

If .Show = -1 Then Folder_Path = .SelectedItems(1)

End With

    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
            If ws.Tab.ColorIndex = 36 Then
                ws.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & ws.Name & ".pdf"
  
                   
    End If
       
    Next
    Call Hide_Colored_Tabs36Duo
    MsgBox "All vieuws duo saved I guess"

End Sub
 

Attachments

  • Cancel pdfduo.JPG
    Cancel pdfduo.JPG
    104 KB · Views: 4
  • Cancel pdfduo2.JPG
    Cancel pdfduo2.JPG
    24.9 KB · Views: 5

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You should be able to add some error handling code, i.e. (new lines added shown in red):
Rich (BB code):
Public Sub TabDuoCreate_PDFs()

'Maakt van alle duo een pdf van het overzicht

Application.ScreenUpdating = False
Call Unhide_Colored_Tabs36Duo

Dim Folder_Path As String

On Error GoTo err_check
With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Selecteer de gewenste folder"
    If .Show = -1 Then Folder_Path = .SelectedItems(1)
End With
On Error GoTo 0

Dim ws As Worksheet
   
For Each ws In ActiveWorkbook.Worksheets
    If ws.Tab.ColorIndex = 36 Then
        ws.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & ws.Name & ".pdf"
    End If
Next
   
Call Hide_Colored_Tabs36Duo
MsgBox "All vieuws duo saved I guess"
   
Exit Sub


err_check:
    If Err.Number = 1004 Then
        MsgBox "Aborted by User", vbOKOnly
    Else
        MsgBox Err.numebr & ":" & Err.Description
    End If

End Sub
 
Upvote 0
Solution
If they hit Cancel or close the folder selection box, Folder_Path comes back with a "". Just test for that immediately after
If .Show = -1 Then Folder_Path = .SelectedItems(1)

VBA Code:
If Folder_Path = "" Then
    MsgBox "Aborted by user", vbOKOnly, "Error"
    ' Do other stuff here to reset, turn screen updating back on, etc.
    Call Hide_Colored_Tabs36Duo
    Application.ScreenUpdating = True
    Exit Sub
End If
 
Upvote 0
Thx Joe4 and NateSc
Thx for the input.
Ended up with a mix from both, playing around with the suggestions.
This code finaly did what i wanted to do

VBA Code:
Public Sub TabDuoCreate_PDFs()

Application.ScreenUpdating = False
Call Unhide_Colored_Tabs36Duo

Dim Folder_Path As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Selecteer de gewenste folder"
    If .Show = -1 Then Folder_Path = .SelectedItems(1)
End With

Dim ws As Worksheet
  On Error GoTo err_check
 
For Each ws In ActiveWorkbook.Worksheets
    If ws.Tab.ColorIndex = 36 Then
        ws.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & ws.Name & ".pdf"
    End If
Next
  
Call Hide_Colored_Tabs36Duo
MsgBox "All vieuws duo saved I guess"
  
Exit Sub


err_check:
    If Folder_Path = "" Then
        MsgBox "Canceled by user", vbOKOnly
        Call Hide_Colored_Tabs36Duo
    
        
    End If

End Sub

Don't know wich to mark as solution :) So Joe4 ..... feel free to choose.
I'm already thankfull for both suggestions.
 
Upvote 0
You may want to turn screen updating back on inside err_check. Also, you can mark Joe4's as the answer.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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