我的代码没有按照我喜欢的方式运行。我希望在选择另一个组合框值后出现组合框值。例如:
Private Sub ComboBox1_change()
Dim ultimaLin As Long, area As New Collection
Dim Value As Variant, temp() As Variant
Dim lin As Integer
On Error Resume Next
'A linha abaixo identifica a última linha'
ultimaLin = Sheets("Produtos").Range("A" & Rows.Count).End(xlUp).Row
'A linha abaixo refere-se a coluna que contém os dados da lista'
temp = Sheets("Produtos").Range("B2:B" & ultimaLin).Value
For Each Value In temp
If Len(Value) > 0 Then area.Add Value, CStr(Value)
Next Value
For Each Value In area
'Adicionando item ao ComboBox'
userform2.ComboBox1.AddItem Value
Next Value
Set area = Nothing
lin = 2
With Sheets("Produtos")
Do Until .Range("B" & lin).Value = ""
If .Range("B" & lin).Value = userform2.ComboBox1.Text Then
userform2.ComboBox2.AddItem .Range("A" & lin).Value
End If
lin = lin + 1
Loop
End With
userform2.Show
End Sub
我为第一个组合框创建该代码,而我为第二个组合框创建该代码:
Private Sub ComboBox1_change()
ComboBox2.Clear
Set sht = ThisWorkbook.Worksheets("Produtos")
ultimaLin = sht.Cells.Find("*", searchorder:=xlByRows,
searchdirection:=xlPrevious).Row
With Worksheets("Produtos").Range("b2:b" & ultimaLin)
If ComboBox1.Value = vbNullString Then
pesquisado = "*"
Else
pesquisado = ComboBox1.Value
End If
Set c = .Find("*" & pesquisado & "*", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
ComboBox2.AddItem Cells(c.Row, 1).Value
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub
但是当我更改组合框1的值时,组合框2中什么也没有发生。重要的是,第一个代码位于模块中,第二个代码位于用户窗体代码中。