Cancel InputBox

LeonardSterk

New Member
Joined
Mar 21, 2021
Messages
33
Platform
  1. Windows
Hi,

An InputBox appears when clicking on the button, when I type a date and click on "OK" it works perfectly.
Except there's also a "Cancel" button, but when you click on Cancel, it still creates the file but without date...
How to cancel the creation of the new sheet with the cancel button?

VBA Code:
Sub Export()

    Dim PathXLSX As String
    Dim NameXLSX As String

    PathXLSX = "G:\Companies\"
    NameXLSX = "Test" & InputBox("Enter Date") & ".xlsx"

    ThisWorkbook.Sheets("Prices").Copy
    Application.DisplayAlerts = False
    With ActiveWorkbook
        .SaveAs Filename:= _
        PathXLSX & NameXLSX, _
        FileFormat:=51, _
        CreateBackup:=False
        .Close False
    End With
    Application.DisplayAlerts = True

    MsgBox "The file has been saved at " & PathXLSX & NameXLSX

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi
Try
VBA Code:
Sub Export()

    Dim PathXLSX As String
    Dim NameXLSX As String

'    PathCSV = "G:\Companies\"
    NameCSV = "Test" & InputBox("Enter Date") & ".xlsx"
If Len(NameCSV) > 9 Then
    ThisWorkbook.Sheets("Prices").Copy
    Application.DisplayAlerts = False
    With ActiveWorkbook
        .SaveAs Filename:= _
        PathXLSX & NameXLSX, _
        FileFormat:=51, _
        CreateBackup:=False
        .Close False
    End With
    Application.DisplayAlerts = True

    MsgBox "The file has been saved at " & PathXLSX & NameXLSX
End If
End Sub
 
Upvote 0
Hi
Try
VBA Code:
Sub Export()

    Dim PathXLSX As String
    Dim NameXLSX As String

'    PathCSV = "G:\Companies\"
    NameCSV = "Test" & InputBox("Enter Date") & ".xlsx"
If Len(NameCSV) > 9 Then
    ThisWorkbook.Sheets("Prices").Copy
    Application.DisplayAlerts = False
    With ActiveWorkbook
        .SaveAs Filename:= _
        PathXLSX & NameXLSX, _
        FileFormat:=51, _
        CreateBackup:=False
        .Close False
    End With
    Application.DisplayAlerts = True

    MsgBox "The file has been saved at " & PathXLSX & NameXLSX
End If
End Sub
Hi,

It still exports the file but without date
 
Upvote 0
tested here ok
Any way
What about
VBA Code:
Sub Export()

    Dim PathXLSX As String
    Dim NameXLSX As String

    PathCSV = "G:\Companies\"
    NameCSV = "Test" & InputBox("Enter Date") & ".xlsx"
 [B]   If Len(Trim(NameCSV)) <> 9 Then[/B]

        ThisWorkbook.Sheets("Prices").Copy
        Application.DisplayAlerts = False
        With ActiveWorkbook
            .SaveAs Filename:= _
                    PathXLSX & NameXLSX, _
                    FileFormat:=51, _
                    CreateBackup:=False
            .Close False
        End With
        Application.DisplayAlerts = True

        MsgBox "The file has been saved at " & PathXLSX & NameXLSX
[B]    End If[/B]
End Sub
 
Upvote 0
Solution
tested here ok
Any way
What about
VBA Code:
Sub Export()

    Dim PathXLSX As String
    Dim NameXLSX As String

    PathCSV = "G:\Companies\"
    NameCSV = "Test" & InputBox("Enter Date") & ".xlsx"
[B]   If Len(Trim(NameCSV)) <> 9 Then[/B]

        ThisWorkbook.Sheets("Prices").Copy
        Application.DisplayAlerts = False
        With ActiveWorkbook
            .SaveAs Filename:= _
                    PathXLSX & NameXLSX, _
                    FileFormat:=51, _
                    CreateBackup:=False
            .Close False
        End With
        Application.DisplayAlerts = True

        MsgBox "The file has been saved at " & PathXLSX & NameXLSX
[B]    End If[/B]
End Sub
Amazing!
Thank you very much
 
Upvote 0
You are welcome
And thank you for the feedback
Be happy & safe
 
Upvote 0
You are welcome
And thank you for the feedback
Be happy & safe
I'm confused. It worked.. But trying again it does not work anymore..?!
It's back at still exporting when clicking cancel.

This is the code I used..

VBA Code:
Sub Export()

    Dim PathXLSX As String
    Dim NameXLSX As String

    PathXLSX = "G:\Companies\"
    NameXLSX = "Test" & InputBox("Enter Date") & ".xlsx"
    If Len(Trim(NameXLSX)) <> 9 Then

        ThisWorkbook.Sheets("Prices").Copy
        Application.DisplayAlerts = False
        With ActiveWorkbook
            .SaveAs Filename:= _
                    PathXLSX & NameXLSX, _
                    FileFormat:=51, _
                    CreateBackup:=False
            .Close False
        End With
        Application.DisplayAlerts = True

        MsgBox "The file has been saved at " & PathXLSX & NameXLSX
    End If
End Sub
 
Upvote 0
Really strange
Replace

VBA Code:
If Len(Trim(NameXLSX)) <> 9 Then
With
VBA Code:
  If Trim(Join(Split(Join(Split(NameCSV, "Test")), ".xlsx"))) <> "" Then
See how it goes
 
Upvote 0
Really strange
Replace

VBA Code:
If Len(Trim(NameXLSX)) <> 9 Then
With
VBA Code:
  If Trim(Join(Split(Join(Split(NameCSV, "Test")), ".xlsx"))) <> "" Then
See how it goes
Yes. Cancel works!
Except... It cancels everything now D:
When clicking on OK it doesn't create a file nor say the msgbox anymore, basically cancelling everything probably.
 
Upvote 0
What?
I'm testing here all the possibilities
Even If you typed nothing then hit Ok will not export
!!!!!!!!!!!
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,266
Members
449,093
Latest member
Vincent Khandagale

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