I need help with Cancel button

ColdGeorge

Active Member
Joined
Aug 21, 2012
Messages
407
Office Version
  1. 2016
Platform
  1. Windows
Hi all

Cancel button of second InputBox is not working properly, please take a look at this code.
Thanks in advance.

ColdGeorge

VBA Code:
Dim rng As Range
Dim newpdfname As String
Dim pdfpath As String

'Enter range
On Error Resume Next
Set rng = Application.InputBox(Prompt:="Please Select", Title:="Get Range", Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub

'Enter name for file
On Error Resume Next
newpdfname = Application.InputBox(Prompt:="Now, Enter name for file", Title:="Get name", Type:=2)
On Error GoTo 0
If newpdfname = "" Then Exit Sub


pdfpath = ThisWorkbook.Path & "\" & newpdfname

rng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfpath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Using "Cancel" on the Input Box when it is set to Text returns a FALSE value, so use this line instead to exit your code:
VBA Code:
If newpdfname = False Then Exit Sub

Hint: An easy way to see something like this is to simply add a MsgBox that returns the value of that variable after your Exit command:
VBA Code:
MsgBox newpdfname
and see what it returns when you click "Cancel".
 
Upvote 0
Solution
Hi all

This is how I, finally, solve my issue:


VBA Code:
Sub crearpdf()

Dim rng As Range
Dim newpdfname As String
Dim pdfpath As String

'Enter range
On Error Resume Next
Set rng = Application.InputBox("Selecciona el Rango", _
"Selección del Rango", , , , , , 8)
On Error GoTo 0

If rng Is Nothing Then Exit Sub

'Enter name for file
On Error Resume Next
newpdfname = Application.InputBox("Introduce el Nombre del Archivo", _
"Nombramiento del Archivo", , , , , , 2)
On Error GoTo 0

If newpdfname = vbNullString Then
    MsgBox "Proceso Cancelado"
        Exit Sub
End If

If newpdfname = False Then Exit Sub

pdfpath = ThisWorkbook.Path & "\" & newpdfname

rng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfpath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True

End Sub

ColdGeorge
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

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