Getting Selections of Multiselect ListView on VBA UserForm - vba

listview sampleI am using a listview on a user form. I have multi-select enabled. I would like to select multiple items in the listview and then take the value found in some other text boxes on the same form and push those values into the selected items subitems (columns). The listview is somewhat of a new beast to me. I have the listview filling without issue. I am able to push data to a single selected row, but I am at a loss on how to get all the selected rows indexes and then push to all the subitems accordingly. I end up only getting the last selected row.
Thx.

To Get The selected Items, use the code :
ListeCount = myListview.ListItems.Count
For i = 1 To ListeCount
If myListview.ListItems.Item(i).Selected Then
MsgBox myListview.ListItems.Item(i).Text
End If
Next i
EDIT #1 :
ListeCount = myListview.ListItems.Count
For i = 1 To ListeCount
If myListview.ListItems.Item(i).Checked Then
MsgBox myListview.ListItems.Item(i).Text
'Check if The column 2 of item 'i' is empty
If myListview.ListItems.Item(i).SubItems(1) = vbNullString Then
'Fill the empty column with a value
myListview.ListItems.Item(i).SubItems(1) = "Test"
End If
End If
Next i

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

In VBA, why does putting a textbox value into a cell trigger a for loop in another userform subroutine?

I created an userform with 2 textboxes, 3 buttons and a listbox with two columns. If I click on an entry in the listbox, the list entry which is selected gets transfered to two different textboxes.
See the code below:
Private Sub NewSourceListBox_Click()
Dim i As Integer
'Show the selected data in the corresponding text boxes
For i = 0 To NewSourceListBox.ListCount - 1
If NewSourceListBox.Selected(i) Then
'Hide the add button and show the change button
NewSourceBtnChange.Top = 168
NewSourceBtnChange.Visible = True
NewSourceBtnAdd.Visible = False
NewSourcesIDTxtBox.Value = NewSourceListBox.List(i, 0)
NewSourcesSourceTxtBox.Value = NewSourceListBox.List(i, 1)
'Pass on the selected item row to another subroutine
selectedItem = i
Exit For
End If
Next i
End Sub
selectedItem is a global variable created in another module, which I need to use in another subroutine. If I change the entries in the text boxes in the userform and click the change button the following code gets executed.
This code:
Private Sub NewSourceBtnChange_Click()
Dim row As Integer
row = 6257 + selectedItem
'Change the selected data in the list box to the corresponding data in the text boxes
Sheets("Datensätze").Range("A" & row).Value = NewSourcesIDTxtBox.Value
Sheets("Datensätze").Range("B" & row).Value = NewSourcesSourceTxtBox.Value
'Another duplicate entry to make vLookup work
Sheets("Datensätze").Range("C" & row).Value = NewSourcesIDTxtBox.Value
Unload Me
'Unload the new entry user form to repopulate the comboboxes
Unload NewEntryUserForm
NewEntryUserForm.Show
End Sub
If I watch this step by step via F8 then the following happens: As soon as I click the "NewSourceBtnChange" button the corresponding subroutine NewSourceBtnChange_Click() starts. When I reach Sheets("Datensätze").Range("A" & row).Value = NewSourcesIDTxtBox.Value the program jumps to the NewSourcesListBox_Click() routine, executes it two times and jumps back to Sheets("Datensätze").Range("B" & row).Value = NewSourcesSourceTxtBox.Value, then executes the NewSourcesListBox_Click() routine for another two times and jumps back again to the last entry Sheets("Datensätze").Range("C" & row).Value = NewSourcesIDTxtBox.Value and executes the rest of the NewSourceBtnChange_Click() routine.
This makes it impossible to get the new data from the text boxes into their destined cells.
Edit:
Just to make it easier to reconstruct the described behavior, I exported the userform and its code and uploaded it.
Here is what your code is going through (just the important parts):
1) While initializing userform, it populates the listbox with:
.RowSource = "Datensätze!A6257:B" & 6257 + Sheets("Datensätze").Range("F2").Value - 1
2) When you click listbox item, you trigger NewSourceListBox_Click code, populate textboxes with selected items and set item index number to selectedItem variable. (which is handled wrong. You need to declare selectedItem as public variable.)
3) Then you click NewSourceBtnChange which triggers NewSourceBtnChange_Click. It sets row number of your selected item:
row = 6257 + selectedItem
Then you change this very cell using:
Sheets("Datensätze").Range("A" & row).Value = NewSourcesIDTxtBox.Value
which you have used to populate your listbox with:
.RowSource = "Datensätze!A6257:B" & 6257 + Sheets("Datensätze").Range("F2").Value - 1
At this moment, listbox is populated again, but this time it has been already selected so it triggers the NewSourceListBox_Click code.
Whenever you change the RowSource range, if the listbox is selected, it will behave like this. So you need to deselect the listbox item to workaround this.
TL;DR:
After:
row = 6257 + selectedItem
Insert:
NewSourceListBox.Selected(NewSourceListBox.ListIndex) = False
Also to be able to get selectedItem value in other subs, you need to declare it as public variable. Outside of subs, on the very top, insert:
Public selectedItem As Long

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)

The selected row in datagridview checks off the checkbox if the cell contains a text in VB.net

I have a form that adds information from the textbox and checkbox to the datagridview. The datagridview only has 2 columns firstname and member column.
I want the application to checkmark the checkbox which is located outside of the datagridview if the selected row has the text "member" in it in the second column. And un-checkmark the checkbox if the selected row has nothing in the member column
So far I got this which displays the first name in the text box, but does not check mark the checkbox if the row contains a text.
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
FirstNametxt.Text = row.Cells("Column1").Value.ToString
End If
Try this code:
YourCheckBox.Checked = IIf(row.Cells("Column2").Value = "member", True, False)

ListBox with multiselect property loses selected items

I need to change a filter in a PivotTable based on a ListBox with multiselect property = 1 - fmMultiSelectMulti, but when I run my VBA code items that are selected in the ListBox lose the selection.
Here is the code:
Private Sub BtnAtualizarLiberty_Click()
With Sheets("Liberty_Data").PivotTables("DinamicaLiberty1").PivotFields("RSCORE_CGV6")
.EnableMultiplePageItems = True
For Z = 1 To .PivotItems.Count
.PivotItems(Z).Visible = False
For r = 0 To ListRiscoScoreLiberty.ListCount - 1
If ListRiscoScoreLiberty.Selected(r) = True And ListRiscoScoreLiberty.List(r) = .PivotItems(Z).Value Then
.PivotItems(Z).Visible = True
End If
Next
Next
End With
End Sub
I've solved the problem. I was filling the listbox with a pivot table data and when I was changing the pivot fields filter at the any others pivot table, the listbox lost all the selected items.
So, now I'm filling the listbox 'manually', I've made a sheet called "Filters" and I've filled a column with all listbox row and it has solved the problem.
Sorry for the bad english.