title: How to exit sub on InputBox if cancel is selected but continue with code if ok is selected
body: <pre><code>    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 &lt;&gt; False Then ThisWorkbook.SaveAs Filename:=vFile, FileFormat:=xlCS
  
  End Sub
  </code></pre>
  
  <p>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. </p>
  
  <p>The code I have above exits sub no matter if OK or Cancel is selected. 
  Without the <code>If x = "" Then Exit Sub</code> 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.</p>
  
  <p>I have tried variations of the code with reference to the link:</p>
  
  <p><a href="https://stackoverflow.com/questions/19609479/handle-cancellation-of-inputbox-to-select-range">Handle cancellation of InputBox to select range</a></p>
  
  <p>I have also tried incorporating the below code, however it leads to exit sub no matter if OK or cancel is selected:</p>
  
  <pre><code>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&lt;&gt;""
   'if enters here the user entered some text
   end if
  
   end sub 
  </code></pre>