Sub Export_Click()
Dim Rng As Range
Dim WorkRng As Range
Dim vFile As Variant
Dim x As Range
On Error Resume Next
TitleId = "Export to CSV file"
x = WorkRng
Set WorkRng = Range("D12:I100")
Set WorkRng = Application.InputBox("Range", TitleId, WorkRng.Address, Type:=8)
If x = "" Then Exit Sub
Application.ActiveSheet.Copy
Application.ActiveSheet.Cells.Clear
WorkRng.Copy Application.ActiveSheet.Range("A1")
vFile = Application.GetSaveAsFilename(InitialFileName:=".csv", _
FileFilter:="CSV files (*.csv), *.csv, All files (*.*), *.*")
If vFile <> False Then ThisWorkbook.SaveAs Filename:=vFile, FileFormat:=xlCS
End Sub
I have a macro that exports a set range to CSV file. I would like the macro to Exit Sub if Cancel is selected on the InputBox but to execute the code if OK is selected.
The code I have above exits sub no matter if OK or Cancel is selected.
Without the If x = "" Then Exit Sub
statement the code exports to csv no matter if ok or Cancel is selected. I've tried some other options below and it looks like the issue might be with the input being a range. I'm relatively inexperienced and have run out of ideas searching this site and others.
I have tried variations of the code with reference to the link:
Handle cancellation of InputBox to select range
I have also tried incorporating the below code, however it leads to exit sub no matter if OK or cancel is selected:
sub inputbox_verification()
text=inputbox("type the text")
if StrPtr(text)=0 then
'if it entenrs here then the user pressed "cancel"
endif
if text=""
'if enters here the user left in blank
end if
if text<>""
'if enters here the user entered some text
end if
end sub