Stop code from running after clicking OK to msgbox prompt

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
I am using the following code supplied below.
This issue i have is that if the code is run & there is no value in cell G13 then i see a Run Time Error Document not saved.
I added the additional code as shown in RED but i after clicking OK to the message box i get the same error.
How can i stop the code from continuing once OK has been clicked so the error isnt shown


Rich (BB code):
Private Sub Generate_Pdf_Click()
  Dim sPath As String, strFileName As String
  strFileName = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR SCREEN SHOT PDF\" & Range("G13").Value & ".pdf"
  If Dir(strFileName) <> vbNullString Then
  MsgBox "GENERATED PDF REQUEST" & vbNewLine & vbNewLine & Range("G13").Value & vbNewLine & vbNewLine & "ALLREADY EXISTS", vbCritical + vbOKOnly, "GENERATE PDF FILE MESSAGE"
  Exit Sub
  
  End If
    With ActiveSheet
    If Range("G13") = "" Then
    MsgBox "NO NAME SELECTED IN THE CUSTOMER DETAILS SECTION", vbCritical, "NO CUSTOMER SELECTED MESSAGE"
    End If
    .ExportAsFixedFormat Type:=xlTypePDF, FileName:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
  MsgBox "PDF HAS NOW BEEN GENERATED", vbInformation + vbOKOnly, "GENERATE PDF FILE MESSAGE"
  
  End With
  
  sPath = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR SCREEN SHOT PDF\"
  strFileName = sPath & Range("G13").Value & ".pdf"
  If Dir(strFileName) <> vbNullString Then
    ActiveWorkbook.FollowHyperlink strFileName
  End If
  
  InvoiceClearSheetQuestion.Show

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
hi ipbr21054,

maybe enter
VBA Code:
Exit Sub
directly after the line with the messagebox like in the first instance of check done in the code.

Ciao,
Holger
 
Upvote 0
Solution
Hi ipbr21054,

a slight rearranging done on your code, please have a look:

VBA Code:
Private Sub Generate_Pdf_Click()
  Dim strFileName As String
  
  Const cstrPath As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR SCREEN SHOT PDF\"
  
  With ActiveSheet
    If .Range("G13") = "" Then
      MsgBox "NO NAME SELECTED IN THE CUSTOMER DETAILS SECTION", vbCritical, "NO CUSTOMER SELECTED MESSAGE"
      Exit Sub
    End If
    
    strFileName = cstrPath & .Range("G13").Value & ".pdf"
    If Dir(strFileName) <> vbNullString Then
      MsgBox "GENERATED PDF REQUEST" & vbNewLine & vbNewLine & _
          .Range("G13").Value & vbNewLine & vbNewLine & _
          "ALLREADY EXISTS", vbCritical + vbOKOnly, "GENERATE PDF FILE MESSAGE"
      Exit Sub
    End If
    
    .ExportAsFixedFormat Type:=xlTypePDF, _
                          Filename:=strFileName, _
                          Quality:=xlQualityStandard, _
                          IncludeDocProperties:=True, _
                          IgnorePrintAreas:=False
    MsgBox "PDF HAS NOW BEEN GENERATED", vbInformation + vbOKOnly, "GENERATE PDF FILE MESSAGE"
  End With
  
  If Dir(strFileName) <> vbNullString Then
    ActiveWorkbook.FollowHyperlink strFileName
  End If
  
  InvoiceClearSheetQuestion.Show

End Sub

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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