【问题标题】:find a row # of the selected item in listbox vba在列表框vba中找到所选项目的行#
【发布时间】:2014-05-27 20:31:48
【问题描述】:

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

现在我有一个 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 中选择的内容联系起来

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

【问题讨论】:

    标签: vba excel listbox


    【解决方案1】:

    我找到了解决此问题的方法 - 我制作了一个额外的列表列来在搜索项目时存储 foundrow#。然后我在用户窗体中创建了另一个文本框来输入行#:

    因此,在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
    

    而且效果很好!

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 所以我使用了您提供的以下代码,除了使用 debug.print 之外,我编写了以下代码:worksheets("hs").cells(1,1) = r 它在 hs 中给了我一个值 1。 hs 是我要存储所选项目的行号的隐藏表。问题是,它给了我列表框中的行号,而不是它从(来自 excel 电子表格)找到的行号。例如,如果我有一个名为apple 的条目并且我执行search 并且我找到了苹果,它将所有关于苹果的信息输入到listbox。我的问题是我如何找到从listbox 中选择的apple 的行号?这有意义吗...?
      • 我以为你问“我如何确定列表框中所选项目的行号”?当您单击它们时,您是否尝试保存/查找列表框中包含的工作表单元格?
      • 是的,一旦我从列表框中单击它,我正在尝试查找工作表中项目的行号。列表框在find 函数之后填充,所以我想在列表框中单击found 项后找到它的行号
      • 我建议您修改您的 find 函数并将WorksheetName!A1 之类的内容放入列表框的最后一列(不必向用户显示),然后单击,激活该工作表并选择该范围。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 2013-01-23
      • 1970-01-01
      相关资源
      最近更新 更多