How to Save/Restore selection of ListBox in VBA

It is for Excel programming like Macro, function, formula, Pivot and other platform support..
alpeshviranik
Posts: 81
Joined: Thu Jul 17, 2014 4:58 pm
Contact:

How to Save/Restore selection of ListBox in VBA

Postby alpeshviranik » Sun Jul 27, 2014 9:56 pm

How to save selected values of ListBox using VBA? How to restore previous selection of ListBox in VBA? Can i get VBA code for the same?


Guest

Re: How to Save/Restore selection of ListBox in VBA

Postby Guest » Mon Jul 28, 2014 5:53 pm

I have used below code and its working fine. The first subroutine will save the selected value in column A. Second subroutine restore the column A value in ListBox.
Save List Selection using below VBA code

Code: Select all

Dim itemSele As Long
   dim startRow as integer
   startRow=1
        For itemSele = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(itemSele) = True Then
        Worksheets("WorksheetName").Range("A" & startRow).Value = ListBox1.List(itemSele)
        startRow = startRow + 1
        End If
        Next

VBA code to To restore the Previous selected values in ListBox

Code: Select all

 Dim LastRowA As String
        Dim itemSele As Long
       Dim startRow as integer
        LastRowA = ActiveSheet.Cells(Rows.count, "A").End(xlUp).row
        startQue = 0
        For itemSele = 0 To ListBox1.ListCount - 1
        For startRow = 1 To LastRowA
        If Worksheets("WorksheetName").Range("A" & startRow).Value = ListBox1.List(itemSele) Then
        ListBox1.Selected(itemSele) = True
        Exit For
        End If
        Next j
        Next
        End If


Return to “Microsoft Excel and VBA Forum”

Who is online

Users browsing this forum: No registered users and 1 guest