Solved

VBA How to select combo box value based on given value

Posted on 2013-12-28
2
2,445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

691 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