If instead of GoTo

eznsd

New Member
Joined
Oct 29, 2015
Messages
42
Code:
Sub ProcessBOM()
    Dim Boards As Long
    Dim Cancel As Boolean
    
    Sheets("DK").Select
    
    Range("Boards").value = Application.InputBox _
    (Prompt:="How many boards are to be assembled?", _
    Title:="Number of Boards", _
    Type:=1)
    Cancel = Range("Boards").value
    
    If Cancel = False Then
        MsgBox "The Cancel button was pressed, exiting"
        Exit Sub
    Else
        Call CopyFormulas
        Call Copy
        Call DeleteBlankRows
        Call DeleteNoDK
        Call dkpn
        Call CleanUp
        Call StockCheck
        Call Total
    
        Sheets("BOM").Activate
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:=Range("A260").value
        Application.DisplayAlerts = True
    
        MsgBox "The BOM processing has completed and the file has been saved"
    End If
End Sub
Hi All. I have this code to call different subs. The user is prompted to enter a number via an input box. If the user hits cancel, I would like to exit the sub. When cancel is pressed, the value in the cell which contains the number is "FALSE". No matter how I rewrite the code, it never exits. I originally had a GoTo statement but I have read a number of posts which say it should be avoided. Any help would be greatly appreciated!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi eznsd,

When you press "cancel" in the inputbox, it's the same as typing nothing.

Code:
dim sCancel as string
sCancel = inputbox("Enter something...")
if sCancel = vbnullstring then
    exit sub
end if
 
Upvote 0
Hi eznsd,

When you press "cancel" in the inputbox, it's the same as typing nothing.

Code:
dim sCancel as string
sCancel = inputbox("Enter something...")
if sCancel = vbnullstring then
    exit sub
end if
Thanks for the quick response Klorrnond! I have tried a few different variations without success. This is one variation. What am I doing wrong?
Code:
Sub ProcessBOM()
    Dim Boards As Long
    Dim sCancel As String
    
    Sheets("DK").Select
    
    Range("Boards").value = Application.InputBox _
    (Prompt:="How many boards are to be assembled?", _
    Title:="Number of Boards", _
    Type:=1)
    sCancel = InputBox("Boards")
    
    If sCancel = vbNullString Then
        MsgBox "The Cancel button was pressed, exiting"
        Exit Sub
    Else
        Call CopyFormulas
        Call Copy
        Call DeleteBlankRows
        Call DeleteNoDK
        Call dkpn
        Call CleanUp
        Call StockCheck
        Call Total
    
        Sheets("BOM").Activate
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:=Range("A260").value
        Application.DisplayAlerts = True
    
        MsgBox "The BOM processing has completed and the file has been saved"
    End If
End Sub
 
Upvote 0
Code:
Sub ProcessBOM()
    Dim val As Long
    
    Sheets("DK").Select
    
    val = Application.InputBox _
    (Prompt:="How many boards are to be assembled?", _
    Title:="Number of Boards", _
    Type:=1)
    
    If val <= 0 Then
        MsgBox "Please enter a valid number"
    Else
        Call CopyFormulas
        Call Copy
        Call DeleteBlankRows
        Call DeleteNoDK
        Call dkpn
        Call CleanUp
        Call StockCheck
        Call Total
    
        Sheets("BOM").Activate
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:=Range("A260").value
        Application.DisplayAlerts = True
    
        MsgBox "The BOM processing has completed and the file has been saved"
    End If
End Sub
Changing my code to this works for both the case when the user inputs 0 or presses cancel.
 
Upvote 0

Forum statistics

Threads
1,215,632
Messages
6,125,909
Members
449,274
Latest member
mrcsbenson

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