+ Reply to Thread
Results 1 to 3 of 3

How to save ListBox Multiselect to Registry

Hybrid View

shedragon713 How to save ListBox... 11-10-2017, 04:10 PM
AlphaFrog Re: How to save ListBox... 11-10-2017, 09:30 PM
Tinbendr Re: How to save ListBox... 11-10-2017, 10:34 PM
  1. #1
    Registered User
    Join Date
    11-07-2017
    Location
    Austin, TX
    MS-Off Ver
    2010
    Posts
    22

    Exclamation How to save ListBox Multiselect to Registry

    Hello,
    I have a multipage userform that has a dozen ListBoxes -- all with MultiSelect. I've saved the selections to a spreadsheet (1 row for each ListBox, each selection in adjacent columns).
    However, when the User reopens the survey, the selections on the userform are all gone.
    How can I save the selections to the Registry so that when the User reopens the userform, the ListBox selections show up?

    Thanks in advance for your help.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: How to save ListBox Multiselect to Registry

    You could read them from the spreadsheet in the Userform_Initialize procedure.

    Example

    Private Sub UserForm_Initialize()
        Dim i As Long
        For i = 1 To 12
            Me.Controls("Listbox" & i).Value = Sheets("Sheet1").Cells(1, i).Value
        Next i
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: How to save ListBox Multiselect to Registry

    You could store them as a customer property.

    Each listbox name would be use to store a string of CSV for later use.

    Each CSV would represent the selected value in the listbox.

    If you had a list of ten in Listbox1 and 1,3 7 items are selected, this would generate a CSV like:

    Listbox1,1,3,7

    which you can parse later with the Split function.

    Private Sub cmdSave_Click()
    Dim Ctrl As Control
    Dim A As Long
    Dim LBAry As String
    
    For Each Ctrl In Me.Controls
        Select Case Ctrl.Name
        Case "ListBox"
            LBAry = Ctrl.Name & ","
            For A = 0 To Ctrl.ListCount - 1
                If Me.Ctrl.Selected(A) Then
                    LBAry = LBAry & A & ","
                End If
            Next
           'Strip off trailing comma.
            LBAry = Left(LBAry, Len(LBAry) - 1)
            StoreVariable Ctrl.Name, LBAry
        End Select
    Next
    
    End Sub
    
    Sub StoreVariable(Varname As String, VarValue As String)
    'http://www.vbaexpress.com/kb/getarticle.php?kb_id=677
     '   ============================================
     '   Save a value in CustomDocumentProperties
     '   ============================================
     '   If the name doesn't exist, we create it and set the initial value to 1
    On Error Resume Next
    Dim cstmDocProp As DocumentProperty
    Set cstmDocProp = ThisWorkbook.CustomDocumentProperties(Varname)
     
    If Err.Number > 0 Then
         
        ThisWorkbook.CustomDocumentProperties.Add _
        Name:=Varname, _
        LinkToContent:=False, _
        Type:=msoPropertyTypeNumber, _
        Value:=VarValue
         '   ========================================================================
         
    Else
         
         '   ========================================================================
         '       if our name exists, we need to store the csv.
        ThisWorkbook.CustomDocumentProperties(Varname).Value = VarValue
         '   ========================================================================
         
    End If
     
     '   Explicitly clear memory
    Set cstmDocProp = Nothing
         
    
    End Sub
    David
    (*) Reputation points appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change a multiselect listbox
    By Yokosuka in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-11-2016, 08:37 AM
  2. [SOLVED] Values from MultiSelect ListBox
    By BVT3030 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-08-2015, 10:56 PM
  3. Copy Selected items from multicolumn, multiselect listbox to another listbox
    By Willigb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2013, 11:27 AM
  4. Listbox (MultiSelect)
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2008, 11:58 PM
  5. Multiselect Listbox use
    By RKS in forum Excel General
    Replies: 1
    Last Post: 05-12-2006, 10:10 AM
  6. Bug in multiselect listbox?
    By Jos Vens in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-08-2006, 04:45 PM
  7. [SOLVED] Last Selection from MultiSelect Listbox
    By Brian in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2005, 03:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1