在列表框vba中查找所选项目的行

在列表框vba中查找所选项目的行,vba,excel,listbox,Vba,Excel,Listbox,如何在列表框中找到所选项目的行 现在我有一个子系统,它将所有找到的项目输入到列表框中,见下文 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("

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

现在我有一个子系统,它将所有找到的项目输入到列表框中,见下文

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
如果用户选择列表框中的项目,那么它将把信息填充到userform的文本框中

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\u单击了解如何确定列表框中所选项目的行。一旦我弄明白了这一点,我将编写一个更新子代码,它将在其中定位所选项目的行,并在文本框中重新输入信息并更新该行的信息

当我找到时,我考虑在隐藏的工作表中存储一行。。但我不知道如何将找到的行与列表框中选择的内容链接起来


希望…这是有道理的,如果没有请让我知道

据我所知,您必须循环查看ListBox中的所有行,因为您可以使用Multi-Select

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

据我所知,您必须循环浏览列表框中的所有行,因为您可以进行多重选择

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

我找到了一种方法来解决这个问题——我制作了一个额外的listcolumn来存储搜索项目时找到的行。然后,我在userform中创建了另一个文本框,用于输入行:

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

在listbox1_click子文件中,我添加了以下行:rownumber.value=listbox1.listbox1.listindex,8

并进行了新的子更新,单击并添加了以下行:

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

而且效果很好

我找到了解决这个问题的方法-我制作了一个额外的listcolumn来存储搜索项目时找到的行。然后,我在userform中创建了另一个文本框,用于输入行:

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

在listbox1_click子文件中,我添加了以下行:rownumber.value=listbox1.listbox1.listindex,8

并进行了新的子更新,单击并添加了以下行:

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

而且效果很好

因此,我使用了您提供的以下代码,除了没有使用debug.print之外,我编写了以下代码:worksheetshs.cells1,1=r,它给了我一个hs中的值1。hs是我要存储所选项目行的隐藏工作表。问题是,它给我的是列表框中的行,而不是excel电子表格中的行。例如,如果我有一个名为apple的条目,我进行搜索并找到apple,它会将有关apple的所有信息输入列表框。我的问题是,当从列表框中选择apple行时,如何查找该行?这有意义吗…?我想你问我如何确定列表框中所选项目的行?单击列表框中包含的工作表单元格时,是否试图保存/查找它们?是的,我试图在从列表框单击后查找工作表中的项目行。listbox在find函数后填充,因此我希望在listbox中单击找到的项后找到该项的行。我建议您修改find函数,并放置类似WorksheetName的内容!A1进入列表框的最后一列,不需要显示给用户,然后单击,激活该工作表并选择该范围。因此,我使用了您提供的以下代码,除了使用debug.print之外,我编写了以下代码:worksheetshs.cells1,1=r,它给了我一个1的hs值。hs是我要存储所选项目行的隐藏工作表。问题是,它给我的是列表框中的行,而不是excel电子表格中的行。例如,如果我有一个名为apple的条目,我进行搜索并找到apple,它会将有关apple的所有信息输入列表框。我的问题是,当从列表框中选择apple行时,如何查找该行?这有意义吗…?我想你问我如何确定列表框中所选项目的行?单击列表框中包含的工作表单元格时,是否试图保存/查找它们?是的,我试图在从列表框单击后查找工作表中的项目行。listbox在find函数后填充,因此我希望在listbox中单击找到的项后找到该项的行。我建议您修改find函数,并放置类似WorksheetName的内容!A1进入列表框的最后一列,无需向用户显示,然后单击,激活该工作表并选择该范围。