Removing All Items From A ComboBox? - vba

How can I programmatically remove all items from a combobox in VBA?

Psuedo code ahead (updated with actual code):
Do While ComboBox1.ListCount > 0
ComboBox1.RemoveItem (0)
Loop
Basically, while you have items, remove the first item from the combobox. Once all the items have been removed (count = 0), your box is blank.
Method 2: Even better
ComboBox1.Clear

You need to remove each one individually unfortunately:
For i = 1 To ListBox1.ListCount
'Remove an item from the ListBox using ListBox1.RemoveItem
Next i
Update - I don't know why my answer did not include the full solution:
For i = ListBox1.ListCount - 1 to 0 Step - 1
ListBox1.RemoveItem i
Next i

The simplest way:
Combobox1.RowSource = "" 'Clear the list
Combobox1.Clear 'Clear the selected text

You can use the ControlFormat method:
ComboBox1.ControlFormat.RemoveAllItems

Best Way:
Combobox1.items.clear();

For Access VBA, if a ComboBox has been populated with a Row Source Type of Value List, I find the following works:
ComboBox.RowSource = ""

For Access VBA, which does not provide a .clear method on user form comboboxes, this solution works flawlessly for me:
If cbxCombobox.ListCount > 0 Then
For remloop = (cbxCombobox.ListCount - 1) To 0 Step -1
cbxCombobox.RemoveItem (remloop)
Next remloop
End If

In Access 2013 I've just tested this:
While ComboBox1.ListCount > 0
ComboBox1.RemoveItem 0
Wend
Interestingly, if you set the item list in Properties, this is not lost when you exit Form View and go back to Design View.

me.Combobox1.Clear
This is the common method

I could not get clear to work. (Mac Excel)
but this does.
ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems

If you want to simply remove the value in the combo box:
me.combobox = ""
If you want to remove the recordset of the combobox, the easiest way is:
me.combobox.recordset = ""
me.combobox.requery

Private Sub cmdClear_Click()
ComboBox1.Value = Null
ComboBox2.Value = Null
End Sub

Related

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

Need help, VBA, need the combobox to avoid entering a specific values

I have a list of data in a sheet from A1:A8 and i have a combobox1 in a userform. What i want is to avoid a user to input an invalid value in the combobox1 based on the lists of data in the sheet
Go to combobox properties and change Style from 0 - fmStyleDropDownCombo to 2 - fmStyleDropDownList
i think this is what you are searching:
https://msdn.microsoft.com/en-us/vba/access-vba/articles/combobox-beforeupdate-event-access
There you have a way to check the values with that you want, there is a example where you can replace it with your range of cells.
Edited (Nice point, i'll put the example here):
Private Sub Combobox_BeforeUpdate(Cancel As Integer)
For i = 1 to 8
If(Cells(i, 1).Value == Me.Combobox.Value)
Cancel = True
Me.Combobox.Undo
End If
Next i
End Sub
Cya.

VBA - Excel Showing form after double click on a Cell

I've encountered a strange Problem. I have an Input Form for the User in Excel.
This Form provides basic CRUD functions to edit a List in a Table. Now this Userform also has a ListBox which shows all the entrys with Id and some basic infos (so you know which entry is which ). Now what I want is, that if you select a row in the normal excel table, and then open the InputForm, the selected row in the table should also be selected in the listBox of the InputForm.
I do this like this:
If Selection.Row >= StartRow() Then
ListBoxAll.listIndex = Selection.Row - StartRow()
Else
ListBoxAll.listIndex = 0
End If
This works great if I open the Ui per Button click. But if I try to open the form with the Before Double Click Event. I got an Offset.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserFormInput.Show
End Sub
Now for the strange part. If I check what value the Selection.Row has, by showing it with a MessageBox, it all works fine! But as soon as I remove the MessageBox its broken all over again.
If Selection.Row >= StartRow() Then
ListBoxAll.listIndex = Selection.Row - StartRow()
MsgBox Selection.Row
Else
ListBoxAll.listIndex = 0
End If
It does not matter where in the if I put the Messagebox.
So the Question has anybody ever got the same Problem ? And does anybody know a solution or a workaround to it ? (I don't want to show a Messagebox!)
So I added some screenshots to make it easier to understand.
Update I think it has something to do with the Focus after a double click, which is (maybe) on the doubleclicked Cell. And gets changed if I put out a MsgBox
(I'll write this as an answer but this is only a part of the code... :)
Oh, german ^.^
I believe each time you call your userform you'll have to compare the selected data to the listbox data. As you have a multicolumn listbox you could do this by the following (I believe, but untested):
Dim ThisRow As Integer
ThisRow = ActiveCell.Row
Dim ThisValue As String
ThisValue = ActiveWorkbook.Sheet("Tabelle1").Cells(ThisRow, 1).Value
This "1" should be the column which contains the same kind of information as is displayed in the first column of your listbox.
Dim i As Integer
For i = 1 to UserFormInput.Listbox1.Listcount
If UserFormInput.Listbox1.List(i, 0).Value = ThisValue Then
UserFormInput.Listbox1.List(i).Select
End If
Next i

Copy listbox to another one

I'm trying to copy a listbox to another one
So I clear all items in the destination listbox like this
While Forms!SalesCallInformation!lstMills.ListCount > 0
Forms!SalesCallInformation!lstMills.RemoveItem (0)
Wend
Then I copy all items like this
For i = 0 To lstMillsToAdd.ListCount - 1
Forms!SalesCallInformation!lstMills.AddItem (lstMillsToAdd.Column(0, i) & ";" & lstMillsToAdd.Column(1, i) & ";" & lstMillsToAdd.Column(2, i))
Next
The problem is that in the destination listbox, I still got the old items.
But when I set a watch on Forms!SalesCallInformation!lstMills.ListCount when I delete I can see that's it's decrementing so it must remove something.
Is it something with refreshing the form? Cause I tried after inserting the new items to do this: Forms!SalesCallInformation.Refresh but I have the same result.
Does anyone have an idea?
Thank you
Also, the rowSourceType is set to value list
This example works for me:
Private Sub Button1_Click()
Me.List2.RowSource = Me.List0.RowSource
End Sub
Private Sub Button2_Click()
Me.List2.RowSource = ""
End Sub
Both listboxes are Value Lists. The Row Source is set to "abc";"def";"ghi"

VB.NET Listbox selectedindex

In my code i select excel files to add them to a listbox, I then run some code that will replace a certain cell on all excel files in the list. Everytime it changes a cell in the excel file it will go to the next one in the listbox using
Me.ListBox2.SelectedIndex = Me.ListBox2.SelectedIndex + 1
But when it reaches the end of the list it gives me an error. How can i let my listbox know that it reached the end.
Put a check like this:
if Me.ListBox2.SelectedIndex + 1 < Me.ListBox2.items.count then
Me.ListBox2.SelectedIndex + = 1
End if
I think I would do a For/Each/Next to loop through the items like this:
For Each ListItem As Object In ListBox2.Items
'Set Active item as selected
ListBox2.SelectedItem = ListItem
'manipulate file
UpdateExcelFile(ListItem.ToString)
Next
Just for fun. You can do this without using IF/END IF
Following code will move the selectedindex to the next item until it reaches the end of the listbox.
ListBox1.SelectedIndex += Math.Abs(CInt(ListBox1.SelectedIndex < ListBox1.Items.Count - 1))