Store and restore selected items in a list box form

most

Board Regular
Joined
Feb 22, 2011
Messages
107
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
I need some help with some code I use in a form.
I have a form were the user can select columns in a sheet and hide and unhide them, that code works fine.
Screenshot_1.png

VBA Code:
Private Sub UserForm_Initialize()
 lCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
 ListBox.ColumnCount = 2
 ListBox.MultiSelect = 2 'Enables to select several values

  With Me.ListBox
    .ColumnWidths = "25;75"
  End With
'Get all column headers name
  For c = 1 To lCol
    ListBox.AddItem
    ListBox.List(c - 1, 0) = Split(Cells(1, c).Address, "$")(1)
    ListBox.List(c - 1, 1) = Cells(1, c).value
  Next c
End Sub

My challenge is that I'm trying to save selected items in the list box to an preset, which the user should be able to restore.
My solution is to store the column letters in an global array, which works fine.
The problem is that I can't get them restored, see Button_Preset2_Click.
VBA Code:
Public preset1 As Object
Private Sub Button_Preset1_Click()
 Set preset1 = CreateObject("System.Collections.ArrayList") 
 'Add column letter of selected items to array
    For i = 0 To ListBox.ListCount - 1
        If ListBox.Selected(i) = True Then
         preset1.Add ListBox.List(i)
        End If
    Next i
 'Just for showing it works
   For i = 0 To preset1.count - 1
       Debug.Print preset1(i)
   Next i
End Sub

Private Sub Button_Preset2_Click()
  For a = 0 To UBound(preset1) 'Loop through all items in array
    For r = 0 To ListBox.ListCount - 1 'Loop through all items in list box
        If IsInArray(preset1(a), preset1) Then ListBox.Selected(r) = True 'If found select it
    Next r
  Next a
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

Second problem is that I would like have the same button for saving and restoring, my solution is to do check if the preset is empty.
But none of the examples below works.
VBA Code:
If Len(Join(preset1)) > 0 Then MsgBox "Preset empty!"
If IsEmpty(preset1) Then MsgBox "Preset  empty!"
If IsNumeric(UBound(preset1)) = False Then MsgBox "Preset  empty!"
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
"System.Collections.ArrayList" has a method to check if an item exists in it, i.e Contains.
Here's an example:

VBA Code:
Sub try15()
Dim dar As Object
Set dar = CreateObject("System.Collections.ArrayList")
 
        dar.Add "a"
        dar.Add "b"

        If dar.Contains("a") Then Debug.Print "YES"
End Sub


So you can use that when you loop through the items in the listbox. This is untested:

VBA Code:
Private Sub Button_Preset2_Click()
 
    For r = 0 To ListBox.ListCount - 1 'Loop through all items in list box
        If preset1.Contains(ListBox.List(r)) Then ListBox.Selected(r) = True   'If found select it
    Next r

End Sub
 
Upvote 0
YEAH, works great! Thanks!
I only had to change it so it deselectes the other lines.

I can't see how I can use ".Contains" to my second issue. But I have bigger issue, the global parameter is not stored outside the session, i'm considering to store it in a sheet instead.

VBA Code:
    For r = 0 To ListBox.ListCount - 1 'Loop through all items in list box
       If preset1.Contains(ListBox.List(r)) Then
          ListBox.Selected(r) = True    'If found select it
        Else
         ListBox.Selected(r) = False    'else deselect it
       End If
    Next r
 
Upvote 0
Sorry, I don't quite understand what you want, can you explain the "sequence"?
Initially the "preset" is blank? then you select some items in the listbox > hit a button > the selected items become the preset? and then what?
But I have bigger issue, the global parameter is not stored outside the session, i'm considering to store it in a sheet instead.
Do you mean you want the preset keeps it's value when you exit the userform?
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top