VBA Input Box in Excel for a Range in a worksheet...

KP_SoCal

Board Regular
Joined
Nov 17, 2009
Messages
116
With my code below you can see I set up a very non-secure means of executing certain commands based on the user names that are inputted.

As you can see, it's not very dynamic at all. I'd like to modify it to pull from a range of cells A2:A20 in worksheet "UserNames".

Any suggestions on how to tweak the code below to accommodate this would be greatly appreciated. Thanks again so much!



Code:
Public Sub Test()

Dim myValue

Dim myIDa As String
Dim myIDb As String
Dim myIDc As String
Dim myIDd As String

myIDa = "Fred" 
myIDb = "Bill" 
myIDc = "Jane" 
myIDd = "Abe"

myValue = InputBox("Input Authorized Name: ", "Authorization Confirmaton", )

If UCase(myValue) = myIDa Or UCase(myValue) = myIDb Or UCase(myValue) = myIDc Or UCase(myValue) = myIDd Then

   MsgBox "Authorized"

ElseIf myValue = "" Then
    Exit Sub

Else
    MsgBox "Unauthorized"

End If

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
With my code below you can see I set up a very non-secure means of executing certain commands based on the user names that are inputted.

As you can see, it's not very dynamic at all. I'd like to modify it to pull from a range of cells A2:A20 in worksheet "UserNames".

Any suggestions on how to tweak the code below to accommodate this would be greatly appreciated. Thanks again so much!

Try this in a standard module.

Your list of names is here... ("Sheet5").Range("A2:A6") and you will adjust to suit your workbook, as well as the MsgBox texting advice to say what you want.

Howard

Code:
Option Explicit

Sub Test_Name()
    On Error Resume Next
    Dim aName As String

    aName = Application.inputbox(Prompt:="Please enter a name:", Title:="A Name of your choice", Type:=2)

    If aName <> "" Then
    
        If OnList(aName) Then
            MsgBox "Your input was " & aName
        
          Else
          
            MsgBox "            " & aName & vbCr & vbCr & _
                   "       Is not included" & vbCr & _
                   "in the authorization list!"
        End If
        
    End If
End Sub
Function OnList(sTxt As String) As Boolean
    On Error Resume Next
    Dim aList As Variant
    aList = ThisWorkbook.Worksheets("Sheet5").Range("A2:A6").Value
    OnList = Not IsError(Application.WorksheetFunction.Match(Trim(sTxt), aList, 0))
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,276
Messages
6,171,138
Members
452,381
Latest member
Nova88

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