Solved

VBA How to select combo box value based on given value

Posted on 2013-12-28
2
2,190 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This collection of functions covers all the normal rounding methods of just about any numeric value.
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

778 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