ListBox with multiselect property loses selected items - vba

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.

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

Getting Selections of Multiselect ListView on VBA UserForm

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

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

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)

VBA - Listbox and Data

I have a listbox called listbox1 that shows me the data in sheet1 but the problem is that it doesn't show me all of the data sometimes i have 3000 line of data and it shows me anlo first 50 line this is the code i am using if someone can help please :
ListBox1.List = Sheets("Sheet1").Range("A1:F3000").Value
if you need datas from one column only then you'd go:
ListBox1.List = Application.Transpose(Sheets("Sheet1").Range("A1:A3000")) '<== change "A"s in Range("A1:A3000") to wanted column index
otherwise if you want to list data from more columns you have to go like this:
Private Sub UserForm_Initialize()
Dim Data As Variant
...
Data = Sheets("Sheet1").Range("A1:F3000")
With UserForm1.ListBox1 '<== change it with actual UserForm and ListBox names
.ColumnCount = 2
.List = Data
End With
but you'd be able to select one or more (up to ListBox MultiSelect property setting) entire row, and not single "cells".
Try changing the properties of the listbox to the range you need. Then change ColumnCount to 6.
Ie:
To make the RowSource dynamic, input 6 on the ColumnCount proterty and delete the range in the RowSource property and add this code to the form.
Private Sub UserForm_Initialize()
ListBox1.RowSource = Sheets("Sheet1").Range("A1:F3000").Address(external:=True)
End Sub