Userform listbox - recall selection

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
Is it possible to recall the value selected (highlighted blue) in a userform listbox when form is shown again.
Below code uses arrays to show subfolders in a listbox.


VBA Code:
Option Explicit

Private Sub UserForm_Initialize()
Dim file_system As Object, sub_folder As Object
Dim main_folder As String
Dim arr_path As Variant
Dim m As Long
    Set file_system = CreateObject("Scripting.FileSystemObject")
    main_folder = "C:\My Documents\" 'Change as required
    'Array Folderpaths
    m = 0
    ReDim arr_path(0)
    If file_system.FolderExists(main_folder) Then
        For Each sub_folder In file_system.GetFolder(main_folder).SubFolders
            arr_path(m) = file_system.GetAbsolutePathName(sub_folder)
            m = m + 1
            ReDim Preserve arr_path(m)
        Next sub_folder
    End If
    'Populate listbox
    For m = 0 To UBound(arr_path)
        ListBox1.AddItem arr_path(m)
    Next m
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
If you only want to make this work on the current session, then you can declare a public variable, and use it to store the existing selection as an integer value. It will be used during the current session.

The following goes into the Thisworkbook class module:
VBA Code:
Option Explicit
Public selectedItem As Integer

Private Sub Workbook_Open()
    selectedItem = -1
End Sub

And this is the addition to your existing code including theChange event procedure of ListBox1 control.
VBA Code:
Private Sub UserForm_Initialize()
    ' Your existing code
    
    ' Add the following
    If ThisWorkbook.selectedItem <> -1 And ListBox1.ListCount > ThisWorkbook.selectedItem Then
        ListBox1.Selected(ThisWorkbook.selectedItem) = True
    End If
End Sub

Private Sub ListBox1_Change()
    ThisWorkbook.selectedItem = ListBox1.ListIndex
End Sub

If you need to save the selection to make it available when it is opened on the same computer next time, then you can use GetSetting and SaveSetting statements to save the selection in the Windows registry.

Beyond this, if you want to save the selection with the workbook and even make it available on other computers, then you'll need to save the value in the workbook itself (somewhere hidden in a worksheet cell, or workbook property, etc.)
 
Upvote 0
I don't know what I was thinking, but obviously, I missed basic math. We don't need to initialize the public variable value in the Workbook_Open event procedure. The following is the optimized code - just in case:

The Thisworkbook class module:
VBA Code:
Option Explicit
Public selectedItem As Integer

Userform:
VBA Code:
Private Sub UserForm_Initialize()
    ' Your existing code
    
    ' Add the following
    If ThisWorkbook.selectedItem > 0 And ListBox1.ListCount > ThisWorkbook.selectedItem Then
        ListBox1.Selected(ThisWorkbook.selectedItem - 1) = True
    End If
End Sub

Private Sub ListBox1_Change()
    ThisWorkbook.selectedItem = ListBox1.ListIndex + 1
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

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