+ Reply to Thread
Results 1 to 4 of 4

Pre-selecting Listbox value from cell value in Userform

Hybrid View

  1. #1
    Registered User
    Join Date
    07-17-2019
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    27

    Pre-selecting Listbox value from cell value in Userform

    Good Morning All!

    I am loading a userform that gets populated with a number of fields from a table depending on the currently selected row.

    I would like to use a listbox in the userform to give the user options to change a cell in that row. Effectively using a flat database and a basic front end to edit the data.

    I am struggling to get the listbox to be pre-selected with a value.

    I am populating the listbox with a dynamic named range to enable more options to be added in the future.

    Dim rRange As Range
    Dim rCell As Range
    Dim lCount As Long
    
    Set rRange = Range("STATUStbl")
    
        For Each rCell In rRange
            ListBox1.AddItem rCell.Value
        Next
    the above code successfully populates the listbox, the below code doesn't successfully autoselect the value in column 2 of the table!

         
    With ListBox1
            For lCount = 0 To .ListCount - 1
                If (.Column(2, lCount)) = Cells(ActiveWindow.RangeSelection.Row, 2) Then
                        .Selected(lCount) = True
                Else
                        .Selected(lCount) = False
                End If
            Next
    End With
    Please see attached, and please help!
    Attached Files Attached Files

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

    Re: Pre-selecting Listbox value from cell value in Userform

    Private Sub UserForm_Initialize()
    
    Dim v As Variant
    'Dim rRange As Range
    'Dim rCell As Range
    'Dim lCount As Long
    
    'Set rRange = Range("STATUStbl")
    '
    '    For Each rCell In rRange
    '        ListBox1.AddItem rCell.Value
    '    Next
        
    With ListBox1
    
            .List = Range("STATUStbl").Value
            v = Application.Match(ActiveCell.EntireRow.Cells(1, 2).Value, .List, 0)
            If Not IsError(v) Then .Selected(v - 1) = True
    '        For lCount = 0 To .ListCount - 1
    '            If (.Column(2, lCount)) = Cells(ActiveWindow.RangeSelection.Row, 2) Then
    '                    .Selected(lCount) = True
    '            Else
    '                    .Selected(lCount) = False
    '            End If
    '        Next
    End With
        
    
    'Position the resize icon
    lblResizer.Left = Me.InsideWidth - lblResizer.Width
    lblResizer.Top = Me.InsideHeight - lblResizer.Height
    minHeight = 354.75
    minWidth = 450
    
    
    End Sub
    Last edited by AlphaFrog; 11-19-2019 at 08:05 AM.
    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
    Registered User
    Join Date
    07-17-2019
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    27

    Re: Pre-selecting Listbox value from cell value in Userform

    AlphaFrog,

    Can't thank you enough. I'm not looking for a lesson, but can you see where I was coming from and can you tell me why it wasn't working??????

    Best, Pete

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

    Re: Pre-selecting Listbox value from cell value in Userform

    You're welcome.

    If (.Column(0, lCount)) = Cells(ActiveWindow.RangeSelection.Row, 2) Then

+ 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. [SOLVED] De-selecting on Listbox Query (When There Are Two Listboxes On Userform)
    By Necroscope in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-10-2019, 08:50 AM
  2. [SOLVED] Listbox selecting another first item after modification on sheet trough userform
    By dark_prince69 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-24-2018, 06:09 PM
  3. Userform Selecting listbox items erroneously
    By pjwhitfield in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 05-25-2015, 04:11 PM
  4. userform listbox
    By tsiguy96 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-14-2013, 11:09 PM
  5. Listbox to Listbox, no duplicates & submitting same UserForm data for each Listbox entry.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 12:18 PM
  6. Listbox - automatically selecting an option based on a cell's value
    By Smurlos in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2011, 01:21 PM

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