vendredi 5 octobre 2018

VBA Checkbox to Listbox (uncheck option to remove array)

I am struggling with a simple thing and cannot resolve it. I have a userform that user can populate from a textbox manually. I decided to add a checkbox as well to allow the user to populate the same listbox with a specific list of items. To do it,I made a simple checkbox with array. It works perfectly fine. But obviously keeps adding the items every time you check and uncheck it.

Private Sub CheckBox1_Click()

Dim interrlist(7) As String

Dim i As Long

interrlist(0) = "who"
interrlist(1) = "when"
interrlist(2) = "how"
interrlist(3) = "why"
interrlist(4) = "by whom"
interrlist(5) = "sent"
interrlist(6) = "memo"
interrlist(7) = "How"


For i = 0 To 7

If CheckBox1.Value = True Then

NegKeyList.AddItem interrlist(i)

End If

Next i


End Sub

I can populate the list when the checkbox is checked with the code above, but struggling to remove the array items from the list when the user unchecks the listbox. I simply need to remove the same items from listbox when user unchecks the same checkbox.

I tried the following solution, but seem to be making something very wrong with it, I understand. Just totally stuck....maybe the solution is much simplier? Could someone help me please?

If checkobx.value=false then

For i = 0 To 7
For j = 0 To NegKeyList.ListCount - 1

If InStr(NegKeyList.List(j), interrlist(i)) > 0 Then
NegKeyList.RemoveItem interrlist(i)

End If

Next j
Next i

end if




Aucun commentaire:

Enregistrer un commentaire