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!
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

Excel Ent

New Member
Joined
Aug 21, 2019
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Very nice! You are a master of the craft...…... Love the sub name.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,924
Messages
5,621,629
Members
415,848
Latest member
LCC

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
Top