Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

how to find something in list

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Grd - 06 Nov 2006 20:26 GMT
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
Jezebel - 06 Nov 2006 20:49 GMT
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.

> Hi,
>
[quoted text clipped - 8 lines]
>
> Suzanne
Dave Lett - 06 Nov 2006 20:54 GMT
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

> Hi,
>
[quoted text clipped - 8 lines]
>
> Suzanne
Go - 06 Nov 2006 21:24 GMT
I like this one

thanks

> Hi Suzanne,
>
[quoted text clipped - 24 lines]
> >
> > Suzanne
Helmut Weber - 06 Nov 2006 21:12 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.