Comma-separated states in cell -> List Box on Open

paipimenta

Board Regular
Joined
Apr 7, 2010
Messages
103
Hello Excel peeps,

I've got an ActiveX ListBox (MultiSelectExtended select mode) with a state list. User can select multiple states, which I output into a cell with:

Code:
(In Private Sub ListBox1_Change() )
    Worksheets("Master - FUW").Range("States").Value = _
        Join(lbSelectedItems, ", ")
(PS: Is there an inverse of Join( ) ? Like ArrayLoad(csv-string,array) ? )
I load lbSelectedItems like so:

Code:
(Also in Private Sub ListBox1_Change() )
    ' Cycle through listbox and load selected items into string array
    For i = 0 To ActiveSheet.ListBox1.ListCount - 1
        If ActiveSheet.ListBox1.Selected(i) = True Then
            intSelectedCounter = intSelectedCounter + 1
            ReDim Preserve lbSelectedItems(0 To intSelectedCounter)
            lbSelectedItems(intSelectedCounter) = ActiveSheet.ListBox1.List(i)
        End If
    Next

Now, unfortunately I have to reload the listbox every time I open the workbook (right?). This is how I do that:

Code:
Private Sub Workbook_Open()
    For i = 1 To 53
        Worksheets("Master - FUW").ListBox1.AddItem ActiveWorkbook.Worksheets(1).Cells(i, 7).Value
    Next i
End Sub

How can I take the comma-separated value from the cell and re-select the states that were selected before? Here's a beginning....

Code:
(In Private Sub Workbook_Open() )
    Dim statesCell As String
    Dim allStates() As String
    ' Set statesCell to value of States cell
    statesCell = Range("States").Value
    ' Reload ListBox1
    For i = 1 To 53
        Worksheets("Master - FUW").ListBox1.AddItem ActiveWorkbook.Worksheets(1).Cells(i, 7).Value
    Next i
    ' Load allStates() from statesCell
    ' Mark members of allStates() as selected in ListBox1
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Here's how I resolved this, in case searchers/Googlers find this later....
This works because I'm using 2 character state abbreviations ALWAYS.
Code:
    ' Load states in States cell into array
    Dim allStates() As String
    Dim statesCell As String
    Dim statesEmpty As Boolean
    Dim i, j, numStates, arrayCounter As Integer
    statesCell = Range("States").Value
    If Len(statesCell) = 0 Then
        statesEmpty = True
        numStates = 0
    Else
        statesEmpty = False
        numStates = Len(statesCell) / 4 - 0.5 + 1 ' works for comma+space-separated abbrevs.
    End If
    arrayCounter = 1
    If Not statesEmpty Then
        For i = 0 To numStates - 1
            If i = numStates - 1 Then
                ReDim Preserve allStates(0 To arrayCounter)
                allStates(arrayCounter - 1) = Right(statesCell, 2)
                arrayCounter = arrayCounter + 1
            Else
                ReDim Preserve allStates(0 To arrayCounter)
                ' works for comma+space-separated abbrevs.
                allStates(arrayCounter - 1) = Mid(statesCell, 4 * i + 1, 2)
                arrayCounter = arrayCounter + 1
            End If
        Next i
    End If
    ' allStates array loaded
    
    ' Load states into ListBox
    For i = 1 To 53
        Worksheets("Master - FUW").ListBox1.AddItem ActiveWorkbook.Worksheets(1).Cells(100 + i, 13).Value
    Next i
    
    If Not statesEmpty Then
        For j = 0 To Worksheets("Master - FUW").ListBox1.ListCount - 1
            For i = 0 To UBound(allStates) - 1
                If allStates(i) = Worksheets("Master - FUW").ListBox1.List(j) Then
                    Worksheets("Master - FUW").ListBox1.Selected(j) = True
                End If
            Next i
        Next j
    End If
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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