Vincular 2 ComoBox en 1 UserForm con VBA (Excel 2007)
Hola. No he podido vincular 2 ComoBox. Tengo un UserForm con 2 ComboBox. El objetivo es que en el primer ComboBox se desplegue "Capitales" y una vez que el ComboBox1 haya hecho Change, el ComboBox2 (con Enabled = True) muestre "Pueblos" asociados a esas "Capitales".
Operativamente, en Hoja1 (A1:A8) están las Capitales (con algunos registros repetidos) y en Hoja1(B1:B8) estàn los pueblos. Estoy ocupando el siguiente còdigo para el primer ComboBox1.
Option Explicit
' This example is based on a tip by J.G. Hussey,
' published in "Visual Basic Programmer's Journal"
Sub RemoveDuplicates()
Dim AllCells As Range, Cell As Range
Dim NoDupes As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item
' The items are in A1:A105
Set AllCells = Hoja1.Range("A1:A8")
' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!
On Error Resume Next
For Each Cell In AllCells
NoDupes.Add Cell.Value, CStr(Cell.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next Cell
' Resume normal error handling
On Error GoTo 0
' Sort the collection (optional)
For i = 1 To NoDupes.Count - 1
For j = i + 1 To NoDupes.Count
If NoDupes(i) > NoDupes(j) Then
Swap1 = NoDupes(i)
Swap2 = NoDupes(j)
NoDupes.Add Swap1, before:=j
NoDupes.Add Swap2, before:=i
NoDupes.Remove i + 1
NoDupes.Remove j + 1
End If
Next j
Next i
' Add the sorted, non-duplicated items to a ListBox
For Each Item In NoDupes
UserForm1.ComboBox1.AddItem Item
Next Item
' Show the UserForm
UserForm1.Show
End Sub
Desde ya GRACIAS!
Operativamente, en Hoja1 (A1:A8) están las Capitales (con algunos registros repetidos) y en Hoja1(B1:B8) estàn los pueblos. Estoy ocupando el siguiente còdigo para el primer ComboBox1.
Option Explicit
' This example is based on a tip by J.G. Hussey,
' published in "Visual Basic Programmer's Journal"
Sub RemoveDuplicates()
Dim AllCells As Range, Cell As Range
Dim NoDupes As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item
' The items are in A1:A105
Set AllCells = Hoja1.Range("A1:A8")
' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!
On Error Resume Next
For Each Cell In AllCells
NoDupes.Add Cell.Value, CStr(Cell.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next Cell
' Resume normal error handling
On Error GoTo 0
' Sort the collection (optional)
For i = 1 To NoDupes.Count - 1
For j = i + 1 To NoDupes.Count
If NoDupes(i) > NoDupes(j) Then
Swap1 = NoDupes(i)
Swap2 = NoDupes(j)
NoDupes.Add Swap1, before:=j
NoDupes.Add Swap2, before:=i
NoDupes.Remove i + 1
NoDupes.Remove j + 1
End If
Next j
Next i
' Add the sorted, non-duplicated items to a ListBox
For Each Item In NoDupes
UserForm1.ComboBox1.AddItem Item
Next Item
' Show the UserForm
UserForm1.Show
End Sub
Desde ya GRACIAS!
1 respuesta
Respuesta de edwinhenao
1