how to find something in list

G

Grd

Hi,

I need to find if an item is in a list through Word VBA for a form I have.

For example the following doesn't work.

If cboListBox.Value In ("Toronto", "Mississauga", "Hamilton") then
cboProvince ="Ontario"

Is there some way to do this. Thanks very much for you help in advance

Suzanne
 
J

Jezebel

Apart from actually iterating the list, you can do things like --

TestString = "|" & cboListBox.Value & "|"
If instr("|Toronto|Mississauga|Hamilton|", TestString) > 0 then ...

(The |s around the string are so you don't get false hits on substrings.)
This is rather ugly programming though, and hard to maintain if the list is
long. Simpler is to set up a list and simply iterate it.

Simpler still, in this case, is to set up the provinces as part of the
original listbox: set it up as a two-column list (town, province), then set
the width of the province column to zero.
 
D

Dave Lett

Hi Suzanne,

This might be a situation where the Select Case statement would be useful:

Select Case cboListBox.Value
Case "Toronto", "Mississauga", "Hamilton"
cboProvince = "Ontario"
Case "Fairfax", "Falls Church", "Springfield"
cboProvince = "Virginia"
Case Else
cboProvince = "No province selected."
End Select

HTH,
Dave
 
H

Helmut Weber

Hi,

there is no a way other than
comparing each item in the one list
against each item in the other list.

Implementations may vary to a great deal,
as Jezebel and Dave have shown.



--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Go

I like this one

thanks

Dave Lett said:
Hi Suzanne,

This might be a situation where the Select Case statement would be useful:

Select Case cboListBox.Value
Case "Toronto", "Mississauga", "Hamilton"
cboProvince = "Ontario"
Case "Fairfax", "Falls Church", "Springfield"
cboProvince = "Virginia"
Case Else
cboProvince = "No province selected."
End Select

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top