VB.NET Listbox selectedindex - vb.net

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

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

Get Selected items from Listbox (main sheet) and display it in a listbox on Userform - VBA

I am trying to get the selected items from a listbox which is on the mainsheet and get those selected items inside a Listbox which is on a user form.
This is the Code,
Sub Viewselectshow()
For lItem = 0 To Sheets("Main").Ent_ListBox.ListCount - 1
If Sheets("Main").Ent_ListBox.Selected(lItem) = True Then
ItemReq = Sheets("Main").Ent_ListBox.Selected(lItem)
ViewSelectedEntitlements.ViewEntitlementListbox.AddItem ItemReq
End If
Next
ViewSelectedEntitlements.Show
End Sub
It works, But it shows a value of -1 in the Listbox on the Userform which is clearly not the Selected item. The Selected Item is a "CaraPhone". Any Suggestions, Kindly share your thoughts.
So you need to use ItemReq = Sheets("Main").Ent_ListBox.List(lItem) instead of ItemReq = Sheets("Main").Ent_ListBox.Selected(lItem)

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.

Removing All Items From A ComboBox?

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