在listbox vba中find所选项目的一行#

我怎样才能find列表框中选定项目的行#?

现在我有一个subinput所有find的项目列表框,见下文

 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 

如果用户select了列表框中的项目,那么它会将信息填充到用户窗体的文本框中

 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所选项目的行号,我将重新input文本框中的信息并更新该行的信息。

我想在隐藏的工作表中存储行#当我find ..但我不知道如何链接find的行与listboxselect的内容

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

我已经find了一个方法来解决这个问题 – 我已经做了一个额外的listcolumn来存储search到的项目时foundrow# 。 然后我做了另一个文本框内的用户将input行#:

因此,在findnext()子文件中,我添加了以下行storerownumber = findnext.row并为列表框添加了另一行。 `listbox1.list(listbox1.listcount-1,8)= storerownumber

并在listbox1_click()子我已经添加了以下行: 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 

它工作正常!

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

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