VBA Checkbox to Listbox (uncheck option to remove array) - vba

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 mylist(7) As String
Dim i As Long
mylist(0) = "time"
mylist(1) = "hour"
mylist(2) = "how"
mylist(3) = "test"
mylist(4) = "number"
mylist(5) = "sent"
mylist(6) = "memo"
mylist(7) = "value"
For i = 0 To 7
If CheckBox1.Value = True Then
Finallist.AddItem mylist(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 after the code, but seem to be making something very wrong with it, I understand. Just totally stuck....Could someone help me please?
If checkobx.value=false then
For i = 0 To 7
For j = 0 To FinalList.ListCount - 1
If InStr(Final.List(j), mylist(i)) > 0 Then
Finallist.RemoveItem mylist(i)
End If
Next j
Next i
end if

Try this (explanations in comments and untested):
If CheckBox1.Value Then ‘ if checkbox checked
For i = 0 To 7
NegKeyList.AddItem interrlist(i)
Next
Else ‘otherwise
Dim i As Long
For i = NegKeyList.ListCount - 1 To 0 Step -1 ‘ loop through listbox items from last one backwards
If Not IsError(Application.Match(NegKeyList.List(i), interrlist,0)) Then NegKeyList.RemoveItem i ‘ if current listbox item matches any interrlist array item then remove it from listbox
Next
End If

Related

VBA "All" checkbox with listbox deselect loop issues

I have a check box that is used for "ALL" (selected by default) selections in a muliselect listbox that is deselected when an item in the listbox is selected. I also coded it so when "ALL" is selected, then it clears the listbox selections and checks the box. It ends up looping through the different subs and makes it annoying for the user.
For instance, when I click an item in the listbox it selects that value, then deselects the checkbox. Since the checkbox is deselected, it goes back through the listbox and deselects the selected item. It loops between the two subs a couple times and ends up only working correctly half the time.
Can I prevent entering the other sub?
Is there better logic so it won't loop as it does?
or maybe an better method to achieve this?
Multiselect listbox:
Private Sub Mkts_Change()
If Me.cheMkts.Value = True Then
Me.cheMkts.Value = False
End If
End Sub
Checkbox:
Private Sub cheMkts_Click()
Dim i As Integer
For i = 0 To Mkts.ListCount - 1
If Me.Mkts.Selected(i) = True Then
Me.Mkts.Selected(i) = False
End If
Next
End Sub
What about adding an If statement around your cheMtks_Click()?
This way when your code deselects it automatically it shouldn't trigger the loop.
Private Sub cheMkts_Click()
If Me.cheMkts.Value = True Then
Dim i As Integer
For i = 0 To Mkts.ListCount - 1
If Me.Mkts.Selected(i) = True Then
Me.Mkts.Selected(i) = False
End If
Next
End If
End Sub
Thanks for your help, Ruben. That corrects the error on the one end, but I am still having issues on the other side. When I have a selection and click the "ALL" box it deselects the check.
I came up with this code, which works beautifully in combo to your suggestion, but only when I have one item selected. If there is anything more, then it still goofs up. Figured I would post to see if you or someone else could advise a solution for multiple selections.
Private Sub Mkts_Change()
Dim i As Integer, count As Integer
For i = 0 To Mkts.ListCount - 1
If Me.Mkts.Selected(i) = False Then
count = count + 1
End If
Next
If Me.cheMkts.Value = True And count <> Mkts.ListCount Then
Me.cheMkts.Value = False
End If
End Sub

VBA excel - adding combo box item to list box

guys I hope someone can help me with this one.
I have a combo box that has data from a named range and I would like to select a value from the combo box and add it to the list box.
Currently I can add an item into the list box with a button but once I add another it overwrites the current item.
Also It needs to be able to add an item at the bottom if the list box already has some values in it.
I think it has something to do with finding the last row but I'm not sure, any help would be highly appreciated :)
image of the issue
Dim i As Integer
With Me.lb_lease
.ColumnCount = 3
.ColumnWidths = "200;50;50"
.AddItem
.List(i, 0) = cbox_hardware.Column(0)
.List(i, 1) = cbox_hardware.Column(1)
.List(i, 2) = cbox_hardware.Column(2)
i = i + 1
End With
I suggest to separate the actions of setting up the listbox and adding items to it. The procedure below will set up the box and clear all existing content. Change the names of the worksheet and the Listbox to match your circumstances. The code will also work if the listbox is in a userform.
Private Sub ResetListBox()
With Worksheets("LibraryAccount").ListBox1
.ColumnCount = 3
.ColumnWidths = "80;50;50"
.Clear ' delete current list
End With
End Sub
The next procedure adds an item to it. It requires a semi-colon separated string, like "One;Two;Three". You might concatenate it from your combobox result using ListIndex to identify the row. The procedure will disassemble the string and add it at the bottom of the list. Worksheet and ListBox names must be changed.
Private Sub AddToListBox(AddArray As String)
Dim Arr() As String
Dim i As Integer
Dim C As Long
Arr = Split(AddArray, ";")
With Worksheets("LibraryAccount").ListBox1
i = .ListCount
.AddItem
For C = 0 To 2
.List(i, C) = Arr(C)
Next C
End With
End Sub
The procedure below is for testing the above procedure. You can run ResetListbox and then call TestAdd several times.
Private Sub TestAdd()
AddToListBox "One;Two;Three"
End Sub

Why is my listbox removing all items instead of what's selected?

I have a set of ListBoxes that pulls data from a report that the rest of the encompassing macro generates. These ListBoxes are displayed in a UserForm adjacent to one another to compare their data together and allow the user to remove data if it matches (refer to picture). Unfortunately, when I press the remove button for the right list (BWListBox), the entire list gets removed rather than just a single item.
I suspect this is due to the dynamic nature of the ListBox (if a Bank Wire report is generated, the list remains single-select and single-column; if a Credit Card Report is generated, it changes to multi-select and double-column, as well as marking a "Total" TextBox as visible, just as the left side)
When I run the Credit Card Report, and use the mark the list (while it's multi-select), the remove button works just fine. Oddly enough, in the single-select form, it removes the entire list.
Here is the block of code for the Remove Button:
Private Sub RemoveButton2_Click()
Dim bwTotal As Double
'If CCReport Then `Currently not functioning due to testing
'bwTotal = BWTotalTextBox.Value 'Throwing error - Double to String ""
'End If
For lItem = BWListBox.ListCount - 1 To 0 Step -1
If BWListBox.Selected(lItem) Then
'GPListBox.RemoveItem GPListBox.ListIndex
BWListBox.RemoveItem lItem
If CCReport Then 'if CCReport since it will be multi-select and multi-column
bwTotal = bwTotal - BWListBox.List(lItem, 1)
BWTotalTextBox.Value = Format(bwTotal, "$#,##0.00")
End If
End If
Next
End Sub
Here is the BWListBox_Change event:
Private Sub BWListBox_Change()
If CCReport Then 'This only exists for the sole purpose of the BWTotalTextbox, if it's not visible (Such as during a Bank Wire Report), there is no need
Dim bwTotal As Double
For lItem = 0 To BWListBox.ListCount - 1
If BWListBox.Selected(lItem) = True Then
bwTotal = bwTotal + BWListBox.List(lItem, 1)
Debug.Print BWListBox.List(lItem, 1)
End If
Next
BWTotalTextBox.Value = Format(bwTotal, "$#,##0.00")
End If
End Sub
EDIT: I recently found it only removes the entire list if the LAST item is selected prior to pressing the remove button.
In case of single select, the loop keeps on running because when you delete one item at the bottom, the immediate item above that gets selected and the loop gets validated.
So, all you need to do is jump out of the loop after deleting the selected element.
For lItem = Me.ListBox1.ListCount - 1 To 0 Step -1
If ListBox1.Selected(lItem) Then
ListBox1.RemoveItem lItem
If Me.ListBox1.MultiSelect = fmMultiSelectSingle Then
Exit For
End If
End If
Next

Listbox Modification in VBA

I created a Userform in Word which imports 3 columns of data from an excel sheet, inserts it into bookmarks and in the name of the word document and saves it as a pdf.
Now I wanted to add a Listbox into the form to be able to add, modify and delete the inputs manually which are usually imported from the excel sheet .
I already figured out how to add data from 3 textboxes into a 3 Column Listbox but even after googling for hours I can't find a good way to modify selected data.
VB.net has the .selectedItem property, VBA does not. Can anybody give me an example how to modify a multi column listbox with the values of 3 textboxes?
Thanks in advance
You need to iterate through ListBox.Selected and check if it is True. Once you get a True one, you can process that item.
Sample code adds some items with columns and sets up a click event to run through the Selected items.
Private Sub ListBox1_Click()
Dim i As Integer
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Debug.Print ListBox1.List(i, 0)
End If
Next i
End Sub
Private Sub UserForm_Initialize()
ListBox1.AddItem "test"
ListBox1.AddItem "test1"
ListBox1.AddItem "test2"
ListBox1.ColumnCount = 3
ListBox1.ColumnHeads = True
ListBox1.List(1, 0) = "change to 1,0"
ListBox1.List(1, 1) = "change to 1,1"
ListBox1.List(1, 2) = "change to 1,2"
End Sub
Picture of form with Immediate window after clicking each item in turn.

Manipulating ListBoxes as Objects

I am working on a VBA userform that includes ListBoxes.
So far, when I had to manipulate one or more, I always proceeded like this in my subs, with dlg as the dialogbox name, and it did not pose any problem, given that I never wanted to do anything complicated:
Dim List1 As Object
...
List1 = dlg.GetControl("CBXname")
...
List1.addItem("String",index)
...
Now I would like to do the following in this Sub
...
If (List1.Exists(Cell1.String) = False) Then
List1.addItem(Cell1.String,k)
End If
...
List1.Clear
...
But I can do neither since List1 is an Object. However, if I decide to declare List1 as a Listbox instead, I do not know how to get the proper control on the ListBox from the dialogbox (the current getcontrol gives me an error).
One of the issues with your code is that listbox objects do not have an "exists" property. To check if a value already exists in your listbox items, you will need to loop through the items.
dim i as integer
for i = 0 to List1.listcount - 1
if List1.column(0, i) = myvalue then
'myvalue exists in List1, skip
else
List1.additem myvalue
end if
next i
Where myvalue is whatever value you are trying to add to the listbox. But that brings us to the second issue in your code which is where you add "Cell1.String". If you are trying to add a value from a worksheet range you will need to refer to that range's value, as worksheet ranges do not have a "string" property as you use it here. Ie. Cell1 = Range("A1").value
As for getting control of the listbox, you can simply refer to the objects name as an object of the form. For example, dlg.List1, if the object's name is List1.
Here is a general purpose routine you can call for any list box. The calling code assumes a list box called ListBox1, a text box called TextBox1, and a Command Button called CommandButton. When you click on the button, it searches the listbox for the text from textbox1.
Private Function ExistsInListbox(ByRef aListBox As msforms.ListBox, ByVal Item As String) As Boolean
Dim booFound As Boolean
booFound = False
Dim t As Integer
ExistsInListbox = False
For t = 0 To aListBox.ListCount - 1 'correction, aListBox not ListBox1
If Item = aListBox.List(t) Then
'if we find a match, short-circuit the loop
booFound = True
Exit For
End If
Next
ExistsInListbox = booFound
End Function
private sub CommandButton_click()
Dim answer As String
Dim val As Boolean
val = ExistsInListbox(Me.ListBox1, TextBox1.Text)
If val Then
answer = "found"
Else
answer = "Not Found"
End If
MsgBox "found-" & answer
End Sub