If equals formula not working as expected

jcornish

New Member
Joined
Jul 21, 2014
Messages
11
Newish to VBA and have stumbled into a problem I can't crack myself.

What the code should do:
I have a series of list box (eTag1) items contained within a cell, which are comma delimited. I use the Split function and then two For loops to run through each of the items in the cell and compare them against the items in the list box (that already been initialised). If there is a match then the code marks the item in the list box as selected.

The problem:
This works fine for the first matched item from the cell, but the code doesn't work for any following matches. When I hover my cursor over Values1(i) and .eTag1.List(j) the tool tip shows the same value, but the next line of code is not executed and the if statement skips to the end.

Appreciate any help!</SPAN>


Code:
 Values1 = Split(Cells(RefRow, 11), ",")
                
            For i = 0 To UBound(Values1)
        
                For j = 0 To .eTag1.ListCount - 1
                
                    If Values1(i) = .eTag1.List(j) Then
                    
                    .eTag1.Selected(j) = True
                                            
                    End If
                
                Next j
             
            Next i
 
jcornish,

Welcome to MrExcel.

Check the ListBox properties and make sure that you have the MultiSelect property set to 1 not 0 ?

Hope that helps.
 
Upvote 0
Hi Tony,

Thanks for your prompt reply.

I have double checked that MultiSelect is set and it is. I use the same user form for data entry to write multiple listbox items to the same cell.

Thanks,

James
 
Upvote 0
James,
Sorry, this isn't going to help a fat lot......
Your code works perfectly well for me both with a listbox in the sheet and a listbox on a form.

Have you got any stray leading / trailing spaces in the split values?

Maybe see if
Code:
  If Trim(Values1(i)) = .eTag1.List(j) Then

does it.
 
Upvote 0
That's great.
Chances are that your cell string is punctuated by 'comma&space' thus when split by comma every element after the first element will have a leading 'invisible' space.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top