简体   繁体   中英

Populating a listbox with a range based on a combobox selection on a userform

Upon picking a selection from a combobox dropdown list I want a listbox to display a specified range of results based on that selection.

It is my understanding that a simple If/Then statement should suffice pending the use of the correct values and properties.

I have one sheet (Sheet1) with two columns.

Column A named "Letters" and Column B named "Numbers.
- In Column A range A2 = A, A3 = B, A4 = C, A5 = D.
- In Column B range B2 = 1, B3 = 2, B4 = 3, B5 = 4.

I would like when range A2 is selected in combobox1 to display range B2 in listbox1.

I would like to use and understand the simplest method.

Here is a non-working example from using this thought process.

Private Sub ComboBox1_Change()
    If Me.ComboBox1.Value = "A" Then
        ListBox1.Value = Range(Sheet1!B2)
    End If        
End Sub

I'm not sure if you want the column A fill of the combo box to be dynamic or not? As a quick demo...

In design mode right click the combo box>Properties>Misc> and then set ListFillRange to A2:A5. This will fill the combobox with data from that range. Double click the combobox to access the code screen. Adapt the following logic according to your own control names:

Private Sub ComboBox1_Change()
  If ComboBox1.Value = "A" Then
     ListBox1.AddItem ActiveSheet.Range("b2")
  End If
End Sub

A => 1

B => 2

ComboBox1.ListIndex will give you the selected item index (Base 0)

So we can write as

ListBox1.Value = Range("B" & (ComboBox1.ListIndex + 2)).Value

I am not very sure of what you wanted, but given that your userform contains a ComboBox named Combobox1 and a ListBox named Listbox1:

Private Sub ComboBox1_Change()
    Dim r As Range
    Set r = [Sheet1!B2]
    ListBox1.Clear
    ListBox1.AddItem r.Offset(ComboBox1.ListIndex).Value
End Sub

Private Sub UserForm_Initialize()
    Dim r As Range
    Set r = [Sheet1!A2:A5]
    ComboBox1.List = r.Value
End Sub

You can add another level of indirection if column B contains Ranges instead of numbers by remplacing:

ListBox1.Clear
ListBox1.AddItem r.Offset(ComboBox1.ListIndex).Value

by

ListBox1.List = Range(r.Offset(ComboBox1.ListIndex).Value).Value

The process followed by me is summarised below. UserForm Initialise Routine set relation between ComboBox and ListBox and Worksheet reference. Though I have genralised for a more general situation of Contact address list which could be of use to many persons.

Private Sub UserForm_Initialize()
    Set rData = ActiveSheet.Range("A1").CurrentRegion
    Me.ComboBox1.List = rData.Offset(1).Value
    Me.ListBox1.ColumnCount = 6
    Me.ListBox1.List = Me.ComboBox1.List
End Sub

ComboBox Change Routine has following code.

Private Sub ComboBox1_Change()
  Me.ListBox1.ListIndex = Me.ComboBox1.ListIndex
End Sub

There is a close Button to close the form.

Private Sub cmdClose_Click() Unload Me End Sub

General declarations as Follows.

Option Explicit
Dim rData As Range

The image of Cotact address Directory is appended below.

[![Contact address directory][1]][1]

在此处输入图像描述

Sample file can be downloaded from here https://www.dropbox.com/s/w5rnp8omkl5u0eu/sample_2409c.xlsm?dl=0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM