DrTribos
asked on
Nested For Loops to Look at 2 ListBoxs in VBA UserForm
Hi All
Happy New Year. I'm attempting to compare items from one listbox with those in another listbox (both on the same userform).
I appear to be using the wrong approach because ListBoxA stops passing values when I introduce a nested ForLoop to look at ListBoxB. Code as follows:
As soon as I uncomment the inner For-Next the MsgBox will not display AItem
This is most alarming!
Cheers, S
Happy New Year. I'm attempting to compare items from one listbox with those in another listbox (both on the same userform).
I appear to be using the wrong approach because ListBoxA stops passing values when I introduce a nested ForLoop to look at ListBoxB. Code as follows:
For i = 0 To myForm.ListBoxA.ListCount - 1
myForm.ListBoxA.ListIndex = i
AItem = myForm.ListBoxA.Value
MsgBox ("List A: " & AItem)
' For j = 0 To myForm.ListBoxB.ListCount - 1
' Next j
Next i
As soon as I uncomment the inner For-Next the MsgBox will not display AItem
This is most alarming!
Cheers, S
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Do I need to reDim AItem? Currently string, also tried DataObject... really I'm just guessing :-(
Sorry, I just noticed you're doing this in MS Word. I assumed you were in Access. Can you upload a copy of this document? Just remove any private information first.
ASKER
Will clean for upload. The other thing I noticed is that if I click in the listbox before running my macro it works perfectly. BUT if I don't click (which would be easily anticipated for this application) it does not work... is there a way to give focus to the list - that might help too...
BTW sorry for reply lag... for some reason my EE emails are not popping up :-(
Thanks
BTW sorry for reply lag... for some reason my EE emails are not popping up :-(
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Graham - works a treat, thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ahhh.... SetFocus is what I was looking for but did not know what to search for. Thanks.
ASKER
Compile Error:
Method or data member not found
I tried changing the i to a 1 for testing purposes (there are 3 items in my list so this should have worked... I have not uncommented the other code.
Cheers,