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
 
What?
I'm testing here all the possibilities
Even If you typed nothing then hit Ok will not export
!!!!!!!!!!!
Ah. It works now, renamed NameCSV to NameXLSX
Now BOTH buttons work!
Many thanks once again. Hopefully u don't see me come back now ...
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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