Solved

VBA How to select combo box value based on given value

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

863 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

18 Experts available now in Live!

Get 1:1 Help Now