Option Explicit
Private Sub ComboBox1_Change()
With Main
.Label1 = .ComboBox1.Column(1)
.ListBox1 = .ComboBox1.Column(2)
End With
End Sub
Private Sub CommandButton1_Click()
Unload Main
End Sub
Private Sub UserForm_Initialize()
With Main.ComboBox1
.RowSource = "MyRang"
.ColumnCount = 2
.ColumnWidths = "50;100"
.ListWidth = 150
End With
End Sub
Và nhân tiện nói đến vấn đề này, em làm sao nó không lấy thông tin được nhỉ!?
Nhờ A/C xem dùm thêm cho em ạ!Mã:Option Explicit Private Sub ComboBox1_Change() With Main .Label1 = .ComboBox1.Column(1) .ListBox1 = .ComboBox1.Column(2) End With End Sub Private Sub CommandButton1_Click() Unload Main End Sub Private Sub UserForm_Initialize() With Main.ComboBox1 .RowSource = "[B][COLOR=#ff0000]MyRang[/COLOR][/B]" .ColumnCount = 2 .ColumnWidths = "50;100" .ListWidth = 150 End With End Sub
MyRang là name ạ. Anh xem file đính kèm nhé!
Option Explicit
Private PriArray
Private Sub ComboBox1_Change()
With ComboBox1
If .MatchFound Then
Dim r As Long
Dim FindArray(1 To 1, 1 To 2)
For r = 1 To .ListCount
If PriArray(r, 1) = .Text Then
FindArray(1, 1) = PriArray(r, 2)
FindArray(1, 2) = PriArray(r, 3)
Exit For
End If
Next
ListBox1.List = FindArray
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim LastRow As Long
Dim MyRange As Range
LastRow = Sheets("dl").Range("A65536").End(xlUp).Row
Set MyRange = Sheets("dl").Range("A2:A" & LastRow)
ComboBox1.List = MyRange.Value
PriArray = MyRange.Resize(, 3).Value
End Sub
Trong bài này thì nó chỉ hiện được một dòng thui. Nếu ô excel có hàng nghì từ thì sao? Em muốn nó hiển thị đầy trong Listbox cơ? Hoặc có thể dụng công cụ nào khác để nó hiển thị được không?Không cần "nem chả" gì cả! Trong UserForm của bạn, code thế này:
Mã:Option Explicit Private PriArray Private Sub ComboBox1_Change() With ComboBox1 If .MatchFound Then Dim r As Long Dim FindArray(1 To 1, 1 To 2) For r = 1 To .ListCount If PriArray(r, 1) = .Text Then FindArray(1, 1) = PriArray(r, 2) FindArray(1, 2) = PriArray(r, 3) Exit For End If Next ListBox1.List = FindArray End If End With End Sub Private Sub UserForm_Initialize() Dim LastRow As Long Dim MyRange As Range LastRow = Sheets("dl").Range("A65536").End(xlUp).Row Set MyRange = Sheets("dl").Range("A2:A" & LastRow) ComboBox1.List = MyRange.Value PriArray = MyRange.Resize(, 3).Value End Sub
OK, nếu vậy thì dùng 2 cái Label, và sự kiện Change của ComboBox sẽ như sau:Trong bài này thì nó chỉ hiện được một dòng thui. Nếu ô excel có hàng nghì từ thì sao? Em muốn nó hiển thị đầy trong Listbox cơ? Hoặc có thể dụng công cụ nào khác để nó hiển thị được không?
Private Sub ComboBox1_Change()
With ComboBox1
If .MatchFound Then
Dim r As Long
For r = 1 To .ListCount
If PriArray(r, 1) = .Text Then
Label1 = PriArray(r, 2)
Label2 = PriArray(r, 3)
Exit For
End If
Next
End If
End With
End Sub
Option Explicit
Private Sub ComboBox1_Change()
With ComboBox1
If .MatchFound Then
Label1 = .List(, 1)
Label2 = .List(, 2)
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim LastRow As Long
LastRow = Sheets("dl").Range("A65536").End(xlUp).Row
ComboBox1.List = Sheets("dl").Range("A2:A" & LastRow).Resize(, 3).Value
End Sub