Solved

VBA How to select combo box value based on given value

Posted on 2013-12-28
2
2,095 Views
Last Modified: 2013-12-31
I have a Word VBA form that contains a combo/drop-down list box. The selections in this box are loaded in the VBA code via an array.

When the form loads I want a particular value to be selected based on the value previously stored in that box which I have saved (for simplicity here) to  a variable called MyValue.

Therefore the selection I want displayed is that with the numerical value MyValue.

The combobox control has two columns, the bound column (numeric) is 1 but only column 2 (text) is displayed (colmn widths 0 pt;200 pt).
For instance the values/displayed text might be:
12/Choice 1
14/Choice 2
17/Choice 3
27/Choice 4
but the actual values in the control can vary each time as can the number and position of the choices.

If say I have a MyValue of 17 then I want Choice 3 displayed.

I can see how to do this using a positional reference, in this case it would be (2), assuming option base zero, but I dont know that MyValue 17 will be in position (2) because this can change.

The question therefore is how to select the displayed value I want based on the MyValue I am passing to the form?
0
Comment
Question by:LawyerLuddite
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39743419
Just step through the List until the value is found
Dim c As Integer
Dim MyValue As String
MyValue = "MyValue"
For c = 0 To ComboBox1.ListCount - 1
    If ComboBox1.List(c, 1) = MyValue Then
        ComboBox1.ListIndex = c
        Exit For
    End If
Next c

Open in new window

0
 

Author Closing Comment

by:LawyerLuddite
ID: 39743441
Thanks. That will do it.  I do find it strange that VBA seem to lack the ability to set values by a simple assignment rather than the inefficient requirement to loop through every value until you get a match.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now