Help to exit VBA upon cancel on input box

MeaclH

Board Regular
Joined
Apr 2, 2014
Messages
77
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
G'day,

I am not a VBA expert. I understand what I want excel and VBA to do for my projects, but I have very simple knowledge of how coding actually works. I get by by asking questions here, which you fine folk are so kind to assist with. I also rehash others VBA code that I stumble across and repurpose where possible.

My current need for help is around exiting a VBA when the cancel button is selected in an input box, or when the input box is left blank.
I have this problem for a couple of VBA's. I will present them all here. Feel free to help with any or all, or if there is a generic string I can add to each code that will work as required.
Thanks heaps for everyone's help on these boards. You help make people like me look good.

VBA Code:
Sub ConfirmExt_click()
Selection.Interior.Color = RGB(215, 245, 215)
       Dim sCmt As String
    Dim rCell As Range

    sCmt = InputBox( _
      Prompt:="Have you confirmed this extra shift or extension with the DAO?" & vbCrLf & _
      "Please add your name, date and time this was confirmed. ", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "No details added. Please start again and include your name, date and time."
    
    Else
        For Each rCell In Selection
            With rCell
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
End Sub

VBA Code:
Sub swap()
    Dim sCmt As String
    Dim rCell As Range, range1 As Range, range2 As Range
    sCmt = InputBox( _
      Prompt:="Is the swap legal?" & vbCrLf & _
      "Please add details of who actioned this swap and when.", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "No details added. Please start again and include your name, date and time."
    
    Else
        For Each rCell In Selection
            With rCell
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
    If Selection.Areas.Count <> 2 Then Exit Sub
    Set range1 = Selection.Areas(1)
    Set range2 = Selection.Areas(2)
    If range1.Rows.Count <> range2.Rows.Count Or _
        range1.Columns.Count <> range2.Columns.Count Then Exit Sub
    range2.Offset(1) = range1.Value
    range1.Offset(1) = range2.Value
    range1.Font.Strikethrough = True
    range2.Font.Strikethrough = True
End Sub

VBA Code:
Sub MarkShiftCovered_click()
Selection.Font.Color = RGB(0, 0, 0)
Selection.Interior.ColorIndex = xlNone
Selection.Font.Underline = False
Selection.Font.Bold = False
Selection.Font.Strikethrough = True
     Dim sCmt As String
    Dim rCell As Range
    
    sCmt = InputBox( _
      Prompt:="Have you covered this shift in it's entirety?" & vbCrLf & _
      "Please add details of how the shift has been covered. ie. OFF roster, extensions.", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "No details added. Please make sure the shift is fully covered."
            
            Else
        For Each rCell In Selection
            With rCell
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
 
End Sub

VBA Code:
Sub Copyandstrike()
Dim r As Integer, c As Integer
   Dim sCmt As String
    Dim rCell As Range
    
    sCmt = InputBox( _
      Prompt:="You are about to mark this shift as open." & vbCrLf & _
      "Please add details on why this shift is now open. ie. absenteeism, training, meetings etc", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "No details added. Please make sure the shift is fully covered."
            
            Else
        For Each rCell In Selection
            With rCell
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing

If Selection.Cells.Count > 1 Then Exit Sub

c = Selection.Column '????*No check on validity of columnn!'

For r = 4 To 8
    If Cells(r, c) = vbNullString Then
        Cells(r, c).Value = Selection.Value
        Cells(r, c).Interior.Color = RGB(255, 124, 128) 'Edit RGB values if necessary
        Selection.Font.Strikethrough = True
        r = 0
        Exit For
     End If
Next

If Not r = 0 Then MsgBox "There are no blanks left!"

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
To exit the sub when a user cancels the inputbox, use the StrPtr function...

VBA Code:
    '   
    '
    '
    
    sCmt = InputBox( _
      Prompt:="Have you confirmed this extra shift or extension with the DAO?" & vbCrLf & _
      "Please add your name, date and time this was confirmed. ", _
      Title:="Comment to Add")
      
    If StrPtr(sCmt) = 0 Then Exit Sub 'user cancelled inputbox
    
    'etc
    '
    '

Hope this helps!
 
Upvote 0
Solution
To exit the sub when a user cancels the inputbox, use the StrPtr function...

VBA Code:
    '  
    '
    '
   
    sCmt = InputBox( _
      Prompt:="Have you confirmed this extra shift or extension with the DAO?" & vbCrLf & _
      "Please add your name, date and time this was confirmed. ", _
      Title:="Comment to Add")
     
    If StrPtr(sCmt) = 0 Then Exit Sub 'user cancelled inputbox
   
    'etc
    '
    '

Hope this helps!
Dom! you the man bro! Thanks heaps
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,532
Members
452,652
Latest member
eduedu

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