anh/chị nào giúp em với ạNho anh chi viet giup em doan code goi y sau khi nhap 1 vai ky tu vao combobox sau khi em da loc danh sach duy nhat trong combobox do a
Tìm theo từ gần nhất theo cột A. Tôi chỉ lấy code trên diển đàn rồi sửa lại cho phù hợp thôi. chứ tôi không rành cho lắm.anh/chị nào giúp em với ạ
Anh không hiểu ý em rồi ạ. Trong file em đính kèm thì combobox đó đã lọc ra danh sách duy nhất trong sheet fin19 rồi ạ, giờ em cần là khi nhập vào 1 ký tự nào đó thì list danh sách trong combobox chỉ hiện thị gới ý theo ký tự nhập vào thôi ạTìm theo từ gần nhất theo cột A. Tôi chỉ lấy code trên diển đàn rồi sửa lại cho phù hợp thôi. chứ tôi không rành cho lắm.
Option Explicit
Private data()
Private Sub ComboBox2_Enter()
If ComboBox2.Tag = "1" Then ComboBox2.List = data
End Sub
Private Sub ComboBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim r As Long, count As Long, strValue As String, result()
If ComboBox2.Tag <> "1" Then Exit Sub
strValue = ComboBox2.text
ComboBox2.Clear
If strValue <> "" Then
strValue = "*" & LCase(strValue) & "*"
ReDim result(1 To 1, 1 To UBound(data, 1) + 1)
For r = 0 To UBound(data, 1)
If LCase(data(r)) Like strValue Then
count = count + 1
result(1, count) = data(r)
End If
Next r
If count Then
ReDim Preserve result(1 To 1, 1 To count)
ComboBox2.Column = result
End If
Else
ComboBox2.List = data
End If
If ComboBox2.ListCount Then ComboBox2.DropDown
End Sub
Private Sub UserForm_Initialize()
Dim lastRow As Long, r As Long, text As String, Arr, dic As Object
With ThisWorkbook.Worksheets("fin19")
lastRow = .Cells(Rows.count, "A").End(xlUp).Row
If lastRow = 2 Then
ReDim Arr(1 To 1, 1 To 1)
Arr(1, 1) = .Range("A2").Value
ElseIf lastRow > 2 Then
Arr = .Range("A2:G" & lastRow).Value
End If
If IsArray(Arr) Then
Set dic = CreateObject("Scripting.Dictionary")
dic.comparemode = vbTextCompare
For r = 1 To UBound(Arr, 1)
text = Arr(r, 1)
If text <> "" And Not dic.exists(text) Then dic.Add text, ""
Next r
If dic.count Then
data = dic.keys()
ComboBox2.Tag = "1"
End If
Set dic = Nothing
End If
End With
End Sub
Tìm theo từ gần nhất theo cột A. Tôi chỉ lấy code trên diển đàn rồi sửa lại cho phù hợp thôi. chứ tôi không rành cho lắm.
Cảm ơn anh ạ. Đã đúng 99% rồi ạ. Chỉ còn 1 vấn đề là khi em nhập ký tự đầu tiên thì trên ô Combobox không hiển thị ký tự đó ạ. Ví dụ em nhập ký tự N thì ô k hiển thị N lên ạ. Anh xem hình giúp em nhé. Cảm ơn anhThử xem.
Toàn bộ code trong Form.
Mã:Option Explicit Private data() Private Sub ComboBox2_Enter() If ComboBox2.Tag = "1" Then ComboBox2.List = data End Sub Private Sub ComboBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) Dim r As Long, count As Long, strValue As String, result() If ComboBox2.Tag <> "1" Then Exit Sub strValue = ComboBox2.text ComboBox2.Clear If strValue <> "" Then strValue = "*" & LCase(strValue) & "*" ReDim result(1 To 1, 1 To UBound(data, 1) + 1) For r = 0 To UBound(data, 1) If LCase(data(r)) Like strValue Then count = count + 1 result(1, count) = data(r) End If Next r If count Then ReDim Preserve result(1 To 1, 1 To count) ComboBox2.Column = result End If Else ComboBox2.List = data End If If ComboBox2.ListCount Then ComboBox2.DropDown End Sub Private Sub UserForm_Initialize() Dim lastRow As Long, r As Long, text As String, Arr, dic As Object With ThisWorkbook.Worksheets("fin19") lastRow = .Cells(Rows.count, "A").End(xlUp).Row If lastRow = 2 Then ReDim Arr(1 To 1, 1 To 1) Arr(1, 1) = .Range("A2").Value ElseIf lastRow > 2 Then Arr = .Range("A2:G" & lastRow).Value End If If IsArray(Arr) Then Set dic = CreateObject("Scripting.Dictionary") dic.comparemode = vbTextCompare For r = 1 To UBound(Arr, 1) text = Arr(r, 1) If text <> "" And Not dic.exists(text) Then dic.Add text, "" Next r If dic.count Then data = dic.keys() ComboBox2.Tag = "1" End If Set dic = Nothing End If End With End Sub
Tôi quên không hướng dẫn.Cảm ơn anh ạ. Đã đúng 99% rồi ạ. Chỉ còn 1 vấn đề là khi em nhập ký tự đầu tiên thì trên ô Combobox không hiển thị ký tự đó ạ. Ví dụ em nhập ký tự N thì ô k hiển thị N lên ạ. Anh xem hình giúp em nhé. Cảm ơn anh
View attachment 234514
Chào thầy ạ. Cho em hỏi thuộc tính kia là gì thế ạTôi quên không hướng dẫn.
Hãy chọn ComboBox2 và trong cửa sổ Properties sửa thành MatchEntry = fmMatchEntryNone
View attachment 234537
Tôi chỉ hơn bạn mỗi một điểm là tôi chịu khó đọc help thôi.Chào thầy ạ. Cho em hỏi thuộc tính kia là gì thế ạ
strValue = LCase(strValue) & "*"
Cám ơn anh rất nhiều ạ. Đã đúng như em muốn rồi ạ.Tôi quên không hướng dẫn.
Hãy chọn ComboBox2 và trong cửa sổ Properties sửa thành MatchEntry = fmMatchEntryNone
View attachment 234537