Conditional Message Box

Excel Ent

New Member
Joined
Aug 21, 2019
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Hello group
I am trying to figure out how to create a series of message boxes that if three conditions are met, it runs the macro.

1.) First a message box will ask for the user to input a value.
2.) It the user cancels or enters a blank it will exit the sub
3.) The value of the user input will be compared against a range say (A1 THRU A20).
1a.) if the value is not in the range a message box will alert that the value in not in the range and ask the user if they want to try again
1b.) if the user hits yes, it will loop to enter another value, if not it will exit the sub
1c.) if the value is in the range then the macro will run

Appreciate any responses


Kind Regards!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
try something like this
VBA Code:
Sub test5()
    Dim aValue
EnterValue:
    aValue = InputBox("Enter a value")
    If Len(aValue) = 0 Then
        GoTo TheEnd
    ElseIf WorksheetFunction.CountIf(Range("A1:A20"), aValue) = 0 Then
        If MsgBox("Not in range ... try again?", vbYesNo, "???") = vbYes Then GoTo EnterValue Else GoTo TheEnd
    Else
        'Call YourMacro
    End If
Exit Sub
TheEnd:
MsgBox "Cancelled by user"
End Sub

if you experience any problems let me know what type of values are being matched in A1:A20
- VBA may require more information
 
Upvote 0
Another option
VBA Code:
Sub ExcelEnt()
   Dim Ans As Variant
   Dim Flg As Boolean
   
   Do
      Ans = InputBox("Enter a value")
      If Ans = "" Then Exit Sub
      If Application.CountIf(Range("A1:A20"), Ans) > 0 Then
         Flg = True
      End If
      If Not Flg Then If MsgBox(Ans & " Not found. Do you want to try again", vbYesNo) = vbNo Then Exit Sub
   Loop Until Flg
End Sub
 
Upvote 0
Solution
Very nice! You are a master of the craft...…... Love the sub name.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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