excel UserForm列表框选择项并到达行

mrphzbgm  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(92)

过去5个小时我一直在寻找答案,但没有找到。我使用的用户窗体有一个列表框,其中列出了包含特定值的单元格。一切正常。但是,我希望在单击列表框中列出的项目时能够转到该行。下面是我使用的代码。

Private Sub ListBox1_Click()

End Sub

Private Sub UserForm_Initialize()
ListBox1.clear
ListBox1.List = Range("AQ3:AQ255").Value
Call List
End Sub

Private Sub List()
Dim i As Long
' loop backwards through items,
For i = ListBox1.ListCount - 1 To 0 Step -1
    ' check each if it contains meaningful text
    If Trim(ListBox1.List(i) & vbNullString) = vbNullString Then
        ' if not, delete that item
        ListBox1.RemoveItem (i)
    End If
Next i
End Sub
hfwmuf9z

hfwmuf9z1#

使用隐藏列保存行。

Private Sub ListBox1_Click()
    Dim i As Long, r As Long
    With ListBox1
        i = .ListIndex
        If i >= 0 Then
            r = ListBox1.List(i, 0)
            'Cells(r, "AQ").Select
            Rows(r).Select
            Me.Hide
        End If
    End With
End Sub

Private Sub UserForm_Initialize()
    Dim i As Long, ar, n As Long
    ar = Range("AQ1:AQ255").Value
    With ListBox1
       .Clear
       .ColumnCount = 2
       .ColumnWidths = "0"
       
       For i = 3 To UBound(ar)
          If Len(ar(i, 1)) > 0 Then
             .AddItem
             .List(n, 0) = i
             .List(n, 1) = ar(i, 1)
             n = n + 1
          End If
       Next
    End With
End Sub

相关问题