首页 文章

在listbox vba中找到所选项的行#

提问于
浏览
0

如何在列表框中找到所选项目的行数?

现在我有 sub 将所有找到的项目输入列表框,见下文

Sub findnext()
Dim Name As String
Dim f As Range
Dim ws As Worksheet
Dim s As Integer
Dim findnext As Range

Set ws = ThisWorkbook.Worksheets("Master")
Name = surname.Value
ListBox1.Clear
Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues)
Set findnext = f

Do
 Debug.Print findnext.Address
 Set findnext = Range("A:A").findnext(findnext)

 ListBox1.AddItem findnext.Value
 ListBox1.List(ListBox1.ListCount - 1, 1) = ws.Cells(findnext.Row, xFirstName).Value
 ListBox1.List(ListBox1.ListCount - 1, 2) = ws.Cells(findnext.Row, xTitle).Value
 ListBox1.List(ListBox1.ListCount - 1, 3) = ws.Cells(findnext.Row, xProgramAreas).Value
 ListBox1.List(ListBox1.ListCount - 1, 4) = ws.Cells(findnext.Row, xEmail).Value
 ListBox1.List(ListBox1.ListCount - 1, 5) = ws.Cells(findnext.Row, xStakeholder).Value
 ListBox1.List(ListBox1.ListCount - 1, 6) = ws.Cells(findnext.Row, xofficephone).Value
 ListBox1.List(ListBox1.ListCount - 1, 7) = ws.Cells(findnext.Row, xcellphone).Value
'ListBox1.List(ListBox1.ListCount - 1, 8) = ws.Cells(findnext.Row, xFirstName).Value
Loop While findnext.Address <> f.Address

End Sub

如果用户选择列表框中的项目,则它会将信息填充到用户表单的文本框中

Sub ListBox1_Click()

    surname.Value = ListBox1.List(ListBox1.ListIndex, 0)
    firstname.Value = ListBox1.List(ListBox1.ListIndex, 1)
    tod.Value = ListBox1.List(ListBox1.ListIndex, 2)
    program.Value = ListBox1.List(ListBox1.ListIndex, 3)
    email.Value = ListBox1.List(ListBox1.ListIndex, 4)
    SetCheckBoxes ListBox1.List(ListBox1.ListIndex, 5)        '<<<< added
    officenumber.Value = ListBox1.List(ListBox1.ListIndex, 6)
    cellnumber.Value = ListBox1.List(ListBox1.ListIndex, 7)     

End Sub

我想从 ListBox1_click() 中找出如何确定列表框中所选项目的行# . 一旦我弄清楚这一点,我将编码 update sub ,它将重新输入文本框中的信息并更新行的信息 .

当我做 find 时,我想在隐藏的工作表中存储一行#但我不知道如何将 found 的行#与 listbox 中选择的行相关联

希望...这是有道理的,如果不是,请告诉我!

2 回答

  • 2

    我已经制作了一个额外的列表列,用于在搜索项目时存储 foundrow# . 然后我在userform中创建了另一个输入行#的文本框:

    因此,在 findnext() sub中我添加了以下行 storerownumber = findnext.row 并为listbox添加了另一行 . `listbox1.list(listbox1.listcount-1,8)= storerownumber

    listbox1_click() sub中我添加了以下行: rownumber.value = listbox1.list(listbox1.listindex,8)

    并创建了一个新的 sub update_click() 并添加了以下行:

    Dim r As Long
    Dim update As Range
    Dim ws As Worksheet
    
    Set ws = Worksheets("Master")
    rownumber.Value = ListBox1.List(ListBox1.ListIndex, 8)
    r = rownumber
    
    ws.Cells(r, xLastName).Value = surname.Value
    ws.Cells(r, xFirstName).Value = firstname.Value
    ws.Cells(r, xTitle).Value = tod.Value
    ws.Cells(r, xProgramAreas).Value = program.Value
    ws.Cells(r, xEmail).Value = email.Value
    ws.Cells(r, xStakeholder).Value = GetCheckBoxes
    ws.Cells(r, xofficephone).Value = officenumber.Value
    ws.Cells(r, xcellphone).Value = cellnumber.Value
    
    end sub
    

    它工作正常!

  • 1

    据我所知,你必须遍历 ListBox 中的所有行,因为你可以有多选 .

    For r = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(r) Then
            Debug.Print "You selected row #" & r + 1
        End If
    Next
    

相关问题