Removing and Adding Values from list box automatically???

dlherr1988

New Member
Joined
Jun 11, 2009
Messages
4
I'm building a userform that has four list boxes on it which autopopulate the headers from a large dataset. The users can then select (or multi-select) the headers in each list box to filter the data.

I've run into a problem where if listboxA and listboxB have the same selection (say "date") the macro that runs when the user pushes the ok button stops. It makes sense for the user not to be able to make the same selection in multiple list boxes for this macro.

Basically I'm having trouble with the code to remove the value ("date" or any other) from boxes B, C, and D when it is selected in A and then adding that same value back in if it is deselected

Private Sub lbFilter_Change()
For i = 0 To frmPivot.lbFilter.ListCount - 1
If frmPivot.lbFilter.Selected(i) = True Then
frmPivot.lbRow.RemoveItem i
End If
Next i For i = 0 To frmPivot.lbFilter.ListCount - 1
If frmPivot.lbFilter.Selected(i) = False Then
frmPivot.lbRow.AddItem i
End If
Next i
End Sub

Right now if you click "date" in lbFilter it is removed from the remaining list boxes. However if you deselect it the index number for i is added back in (say "1"). This creates a tornado where now removing "revenue" from lbFilter removes "Profit" from the remaining list boxes because the indexes are messed up.

Any help on getting this macro to remove the actual Value and replace that same Value would be much appreciated!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Still stuck on this problem with removing one item from all other list boxes on a user form on selection and then adding back in when de-selected.

I've tried loops, if statements, everything I can think of. Loops work but when something is removed and then added back in the index number is added in rather than the value.
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

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