Link to home
Start Free TrialLog in
Avatar of DrTribos
DrTribosFlag for Australia

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:
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

Open in new window


As soon as I uncomment the inner For-Next the MsgBox will not display AItem

This is most alarming!

Cheers, S
SOLUTION
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DrTribos

ASKER

Thanks for your reply....  I get an error:

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,
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.
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Graham - works a treat, thanks.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahhh.... SetFocus is what I was looking for but did not know what to search for.  Thanks.