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:
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
Edit:
Here's my new code which does not update cell A1048306 but still continues with the vba and updates Cells B1048306, C1048306, D1048306, E1048306 and prints the range.
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
End Sub