VBA code to select a value from a pre-defined Range Named List

Roncondor

Board Regular
Joined
Jun 3, 2011
Messages
79
I have a range name called "LoanOptions" which lists various loan options (surprise, surprise) e.g. Fixed, LIBOR, ARM, ..

Within a macro I want the user to be able to select which option they want. i.e. similar to a dropdown in regular Excel

I googled it but none of the answers relates to my problem.

Thanks!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If you want a VBA solution and don't want to invest the time in creating a userform, you could try something like this:
Code:
Sub PickAnOption()
Dim Options As Variant, Choice As String
Options = Array("FIXED", "LIBOR", "ARM") 'add options to suit using quote marks and a comma delimiter
startOver:
Choice = Application.InputBox("Ehter an option from the following list:" & vbNewLine & Join(Options, vbNewLine), Type:=2)
If Choice = "False" Then Exit Sub
For i = LBound(Options) To UBound(Options)
    If UCase(Choice) = UCase(Options(i)) Then  'Remove the Ucase from both sides of the "=" if you want the choice to be case sensitive
        Exit For
    ElseIf i = UBound(Options) Then
        MsgBox "Your entry must be one of these options:" & vbNewLine & Join(Options, vbNewLine)
        GoTo startOver
    End If
Next i
'do something with the Choice
End Sub
 
Last edited:
Upvote 0
Here's a variation that uses the list of options from a vertical named range named "Options".
Code:
Sub PickAnOption()
'assumes options are in a vertical range named "Options"
Dim Options As Variant, Choice As String
Options = Range("Options").Value
startOver:
Choice = Application.InputBox("Ehter an option from the following list:" & vbNewLine & Join(Application.Transpose(Options), vbNewLine), Type:=2)
If Choice = "False" Then Exit Sub
For i = LBound(Options) To UBound(Options)
    If UCase(Choice) = UCase(Options(i, 1)) Then
        Exit For
    ElseIf i = UBound(Options) Then
        MsgBox "Your entry must be one of these options:" & vbNewLine & Join(Application.Transpose(Options), vbNewLine)
        GoTo startOver
    End If
Next i
'do something with the Choice
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,130
Messages
6,129,058
Members
449,484
Latest member
khairianr

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