Article ID: 112330
Article Last Modified on 10/10/2006
For Each mCell In Selection '"Selection" is the group argument
MsgBox mCell.Value
Next
If the group argument is a list in a custom dialog box, the For Each
command may fail, and you will receive the following error message:
<Object> = <Listname>.Listand then use the object in the For Each statement. For example, instead of
xList = DialogSheets("Dialog1").ListBoxes("List1")
For Each mItem in xList.List
<statements>
Next
use
xList = DialogSheets("Dialog1").ListBoxes("List1")
mTemp = xList.List
For Each mItem in mTemp
<statements>
Next
'----------------------------------------------------------------------
Option Explicit
Sub ForEachListItem()
'Dimension some variables.
Dim Alpha As Variant, Foxtrot As Variant, Golf As Variant
'Set an object name for easy referencing of the list box.
Set Alpha = DialogSheets("Dialog1").ListBoxes("List1")
'Add three items to the list.
Alpha.AddItem "Bravo"
Alpha.AddItem "Charlie"
Alpha.AddItem "Delta"
'Set an object name so that the For Each structure can function
'properly.
Golf = Alpha.List
'Iterate through the loop once for each item in Golf (which is
'the same as iterating once for each item in the list box).
For Each Foxtrot In Golf
'Show the current list item in a message box.
MsgBox Foxtrot
Next 'repeat until all done
End Sub
'----------------------------------------------------------------------
To run the subroutine, position the cursor in the line that reads
"Sub ForEachListItem()," and either press F5 or choose Start from the Run
menu.
Keywords: kbprogramming KB112330