1. 顽石财经首页
  2. 专题归档

VBA窗体Combobox二级联动菜单

'放置位置:窗体
Option Explicit
Private Sub UserForm_Initialize()
    Dim col As New Collection
    Dim arr As Variant
    Dim rng As Range
    Dim i As Integer
    On Error Resume Next
    For Each rng In Range("A2:A" & Sheet1.Cells(Rows.Count, 1).End(xlUp).Row)
        If rng <> "" Then col.Add rng, CStr(rng)
    Next
    ReDim arr(1 To col.Count)
    For i = 1 To col.Count
        arr(i) = col(i)
    Next
    ComboBox1.List = arr
   'ComboBox1.ListIndex = 0
End Sub
Private Sub ComboBox1_Change()
    Dim myAddress As String
    Dim rng As Range
    Dim mymsg As Integer
    ComboBox2.Clear
    With Sheet1.Range("A:A")
        Set rng = .Find(What:=ComboBox1.Text)
            If Not rng Is Nothing Then
                myAddress = rng.Address
                Do
                    ComboBox2.AddItem rng.Offset(0, 1)
                    Set rng = .FindNext(rng)
                Loop While Not rng Is Nothing And rng.Address <> myAddress
            End If
    End With
   'ComboBox2.ListIndex = 0
End Sub

本文来自用户投稿,不代表顽石财经立场;如侵犯了您的权益,请与本站联系;如若转载,请注明出处:https://www.rocknod.com/1364.html

发表评论

登录后才能评论
QR code