For Next Statement with a Combo Box

O

Owen

Is it possible to use a For...Next statement that interrogates the results of
a Combo Box?

I have numerous Combo Boxes called ComboBox1 to ComboBox40. I would like to
test the result of each combo box against an IF statement. Can i use a For
Next loop along the lines of:

For j = 1 to 40
If ComboBox(j).Result = "Test" Then
.......
Next j

Thanks for your help.
 
D

Doug Robbins - Word MVP

What type of comboboxes are they? ActiveX Comboboxes, comboboxes as used on
a UserForm or DropDown Formfields as used in a document that is protected
for filling in forms?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

You don't need a macro to test for a value in one of a number of dropdown
fields. You can do it with a calculated field - see the example at
http://www.gmayor.com/formatting_word_fields.htm - Testing for a value in
one or more of several fields.
Replace the Mergefields in that example with REF fields that call the
bookmark names of the dropdown fields.

If the form is for distribution, it is better to avoid macros as you cannot
guarantee the user will allow them to run but ...

If you want to do it by macro then

For Each oDD In ActiveDocument.FormFields
If oDD.Type = wdFieldFormDropDown Then
If oDD.Result = "Test" Then
MsgBox "A field result contains Test"
Exit For
End If
End If
Next oDD

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
O

Owen

Hi Graham

I might have provided you with misleading information on my previous post.
The ComboBoxes (which contain about 50 entries each) don't have a bookmark
associated with them ie they are called ComboBox1, ComboBox2 etc. They are
actually ComboBoxes as used on a UserForm.

Sorry, new at this and still getting used to the terminology.
 
G

Greg Maxey

Something like this:

Private Sub CommandButton1_Click()
Dim i As Long
Dim oObj As Object
For i = 1 To 40
On Error GoTo Err_Handler
If Me.Controls("ComboBox" & i).Value = "Test" Then
MsgBox "Do something"
End If
Err_ReEntry:
Next
Err_Handler:
Resume Err_ReEntry
End Sub
 
O

Owen

Hi Greg

I have copied my code for reference. I am receiving the message "Invalid use
of Me Keyword".

Hope you can see where i am going wrong.

Thanks again.

Sub SampleType()
Dim j As Long
For j = 26 To 30
If Me.Controls("ComboBox" & j).value = CLAY Then
Selection.GoTo What:=wdGoToBookmark, Name:="Sample1"
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.GoTo What:=wdGoToBookmark, Name:="Result1"
Selection.Paste
End If
Next j
End Sub
 
G

Greg Maxey

Owen,

"Me" refers to the UserForm and the code I provided assumes that UserForm is
loaded and has focus. I used a command button on the UserForm to run the
test.
 

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

Similar Threads


Top