title: How to stop vba after user cancels input box
body: <p>I have written vba code which pops up an input box and then prints a range. I want to stop the code if the user presses the 'cancel' button. As of now, it prints it irrespective of the user action and wastes a lot of paper. Here is the code:</p>
  
  <pre><code>Sub Form()
  ThisWorkbook.Sheets("FAQs").Activate
  ActiveSheet.Range("A1048306").Select
  ActiveCell.FormulaR1C1 = InputBox("SrNo1")
  ThisWorkbook.Sheets("FAQs").Activate
  ActiveSheet.Range("B1048306").Select
  ActiveCell.FormulaR1C1 = InputBox("SrNo2")
  ThisWorkbook.Sheets("FAQs").Activate
  ActiveSheet.Range("C1048306").Select
  ActiveCell.FormulaR1C1 = InputBox("SrNo3")
  ThisWorkbook.Sheets("FAQs").Activate
  ActiveSheet.Range("D1048306").Select
  ActiveCell.FormulaR1C1 = InputBox("SrNo4")
  ThisWorkbook.Sheets("FAQs").Activate
  ActiveSheet.Range("E1048306").Select
  ActiveCell.FormulaR1C1 = InputBox("SrNo5")
  Columns("A:D").EntireColumn.Hidden = False
  Range("A1048308:B1048359").PrintOut
  Columns("A:D").EntireColumn.Hidden = True
  ActiveSheet.Range("A1").Select
  ThisWorkbook.Sheets("Spends Tracker").Activate
  ActiveSheet.Range("A1").Select
  End Sub
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Here's my new code which <strong>does not update cell A1048306</strong> but still continues with the vba and updates Cells B1048306, C1048306, D1048306, E1048306 and prints the range.</p>
  
  <pre><code>Sub Form()
  Dim strVale As String
  
  Dim ws As Excel.Worksheet
  Set ws = ActiveWorkbook.Sheets("FAQs")
  
  strVale = InputBox("SrNo1")
  If strVale = vbNullString Then
      MsgBox ("User canceled!")
      ThisWorkbook.Sheets("Spends Tracker").Activate
      ActiveSheet.Range("A1").Select
      Exit Sub
  Else
      'Here we can use the objects range to set the formula.
      ws.Range("A1048306").FormulaR1C1 = strValue
  End If
  
  ws.Range("B1048306").FormulaR1C1 = InputBox("SrNo2")
  ws.Range("C1048306").FormulaR1C1 = InputBox("SrNo3")
  ws.Range("D1048306").FormulaR1C1 = InputBox("SrNo4")
  ws.Range("E1048306").FormulaR1C1 = InputBox("SrNo5")
  ws.Columns("A:D").EntireColumn.Hidden = False
  ws.Range("A1048308:B1048359").PrintOut
  ws.Columns("A:D").EntireColumn.Hidden = True
  ActiveSheet.Range("A1").Select
  ThisWorkbook.Sheets("Spends Tracker").Activate
  ActiveSheet.Range("A1").Select
  </code></pre>
  
  <p>End Sub</p>