简体   繁体   中英

Excel.VBA.UserForm.MultiComlumnListBox.ListBoxes

I am currently working on a project that requires me to use values from a multicolumn list. I have single column listboxes within this userform that I want a value selected given the selected multicolumn list. As of now, I can get the single listboxes to get highlighted but for some odd reason, the values aren't being selected. I need to be able to pull these values if the user doesn't select another list value. I am new at posting in this forum but I have an excel file to demonstrate my problem but do not know how to post it on here. Since I don't know how to upload the file here's what I've coded:

Private Sub ListBox1_AfterUpdate()
Me.ListBox2.Value = ListBox1.Column(1)
Me.ListBox3.Value = ListBox1.Column(2)
Me.ListBox4.Value = ListBox1.Column(3)
Me.ListBox5.Value = ListBox1.Column(4)
MsgBox ListBox2.Value & " = " ListBox3.Value & " = " ListBox4.Value & " = " ListBox5.Value ' Check values
End Sub

Private Sub UserForm_Initialize()
With ListBox1
.ColumnCount = 15
.RowSource = ActiveSheet.Range("A1:E10").Address
End With

With Sheets("Sheet2")
ListBox2.RowSource = "'" & .Name & "'!" & .Range("Variable1").Address
ListBox3.RowSource = "'" & .Name & "'!" & .Range("Variable2").Address
ListBox4.RowSource = "'" & .Name & "'!" & .Range("Variable3").Address
ListBox5.RowSource = "'" & .Name & "'!" & .Range("Variable4").Address
End With
End Sub

And just so you guys know, the values in the specific columns are limited to the variable ranges. If anyone can help out, it could be truly appreciated. Thanks!

List boxes have the Selected property which is a boolean collection of list items numbered from 0 to ListCount-1 .To pre-select the 3rd item in ListBox5 you would use code like ListBox5.Selected(3)=True . Use False to unselect it.

You aren't quite going about it in a way that is easy to correct. Therefore I have written a little code for you to play with. Please create a userform with one ListBox and call it ListBox2 Paste the code below into the UserForm's code sheet.

Option Explicit

Private Sub UserForm_Click()

    Static i As Integer

    With ListBox2
        .Selected(i) = True
        MsgBox "ListIndex = " & .ListIndex & vbCr & _
               "Value = " & .Value & vbCr & _
               "Column(3) = " & .Column(3)
    End With

    i = i + 1
    If i > 6 Then i = 0
End Sub

Private Sub UserForm_Initialize()

    Dim Rng(2 To 5) As Range
    Dim Id As Integer

    With ActiveSheet
        Set Rng(2) = .Range(.Cells(3, 3), .Cells(8, 7))
        Set Rng(3) = .Range(.Cells(9, 3), .Cells(18, 7))
        Set Rng(4) = .Range(.Cells(19, 3), .Cells(28, 7))
        Set Rng(5) = .Range(.Cells(29, 3), .Cells(38, 7))
    End With

    For Id = LBound(Rng) To UBound(Rng)
        SetListBox Id, Rng
        Exit For            ' for testing: exit after doing the first ListBox
    Next Id
End Sub

Private Sub SetListBox(Id As Integer, Rng() As Range)

    Dim Wdth As String
    Dim i As Integer

    Wdth = "60 pt"
    For i = 2 To Rng(Id).Columns.Count
        Wdth = Wdth & "; 0pt"
    Next i

    With Me.Controls("ListBox" & CStr(Id))
        .RowSource = Rng(Id).Address(External:=True)
        .ColumnCount = Rng(Id).Columns.Count
        .ColumnWidths = Wdth
    End With
End Sub

Run the code on F5 and click anywhere on the form - repeatedly if you like.

The basic arrangement is that there is a list with 5 columns. The number of columns is equal to the number of columns in the source range. The first column is 60 pts wide, the others hidden. The idea is that the user selects an item from the visible column and the other ListBoxes are set according to the values in the hidden columns. I didn't set that up but ListBox2.Column(3) will return the value from column 3 of the selected item.

I hope you have all the syntax that you need to make your idea work. It isn't arranged quite the way you want, but I believe it is all there. Good luck.

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