Lọc không trùng bằng DIC (2 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

NguyenthiH

Thành viên mới đăng ký
Tham gia
11/12/16
Bài viết
965
Được thích
175
Giới tính
Nữ
Chào mọi người.
Ở Sheet1 em có vùng dữ liệu "B2:D", em muốn khi Userform1.Show thì sẻ đập vùng dữ liệu này vào ListBox1(tất nhiên là không trùng). Thê thì code trong Form.Initialize phải là sao ah!
Mã:
Form.Initialize ()
Dim arr(), Dic As Object, i As Long, LastR As Long
  LastR = Sheet1.Range("B65500").End(xlUp).Row
  If LastR > 1 Then
    arr = Sheet1.Range("B2:D" & LastR).Value
    Set Dic = CreateObject("Scripting.Dictionary")
     i = 1 To UBound(arr)
      Dic(arr(i, 1)) = ""
    ListBox1.List=arr
    Set Dic = Nothing
  End If
End Sub
Em phỏng code vậy thôi, chứ cót két em chả biết gì.
Mong mọi người giúp đỡ.
 
Em chắc cú là lỗi khi các Anh đưa Code lên dạng PHP Code, em copy PHP Code Paste vào File Gốc là lỗi.
Bằng chứng là khi em copy code của Anh Hoang2013 và dán lại vào bài #20 dạng
Mã:
(không phải PHP Code) và em copy code bài #20 vào File gốc thì lại được.
Em cũng chả hiểu vì sao.
 
Upvote 0
Cám ơn Anh Hoang2013 và Thầy Ba Tê.
Trong code của Thầy Ba Tê có Sub ListBox1_DblClick(), đúng ý định của em luôn, nhưng mong Thầy chỉnh cho khi double click xong thì Active Cell xuống một dòng để nhập tiếp.
 
Upvote 0
Cám ơn Anh Hoang2013 và Thầy Ba Tê.
Trong code của Thầy Ba Tê có Sub ListBox1_DblClick(), đúng ý định của em luôn, nhưng mong Thầy chỉnh cho khi double click xong thì Active Cell xuống một dòng để nhập tiếp.

Bạn "chơi" kiểu SelectionChange để Show Form. Nếu xuống 1 dòng thì lại kích hoạt SelectionChange để Show Form?
Vậy thì phải đóng Form rồi kích hoạt lại? "Cà giựt, cà giựt ..."?
PHP:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
With Selection
    .Value = ListBox1.List(ListBox1.ListIndex, 0)
    .Offset(, 1).Value = ListBox1.List(ListBox1.ListIndex, 1)
    .Offset(, 2).Value = ListBox1.List(ListBox1.ListIndex, 2)
        Unload Me
    .Offset(1).Select
End With
End Sub
 
Upvote 0
Em chắc là lỗi khi các Anh đưa Code lên dạng PHP Code, em copy PHP Code Paste vào File Gốc là lỗi.
Bằng chứng là khi em copy code của Anh Hoang2013 và dán lại vào bài #20 dạng
Mã:
 (không  PHP Code) và em copy code bài #20 vào File gốc thì lại được.
[/QUOTE]

Một phát hiện mới & lí thú; & nên báo ngay cho BĐH về việc này!

Chúc mọi người vui vẻ & hạnh phúc trong ngày nghỉ cuối tuần. -=09=
 
Upvote 0
Đúng là "Cà Giựt, Cà Giựt", thế thì em chuyển qua nút để hiện Form.
Ah mà cho em hỏi tí, có thể chỉnh code để khi chọn trong listBox bang phím xuống (40) và nhấn Enter thì đập vào Sheet luôn khỏi Double Click được không vậy Thầy?
 
Upvote 0
Anh Hoang2013 nói giỡn hay nói thiệt vậy???
Em mong mọi người Test dùm em, copy Code bài #9 của Anh Hoang2013 vào File Bài #7 của em xem có lỗi không vậy.
Nếu không lỗi là do máy em, thì em phải cài lại Win và Office.
 
Upvote 0
Thích thì chìu thôi!

-\\/. %#^#$ **~** @#!^% //////
 

File đính kèm

Upvote 0
Ý của em là các Anh làm như vậy trên máy các Anh có bị lỗi không, báo cho em biết.
Còn File của Anh Hoang2013 sao em mở bị Protect View.
 
Upvote 0
Ý của em là các Anh làm như vậy trên máy các Anh có bị lỗi không, báo cho em biết.

Còn File của Anh Hoang2013 sao em mở bị Protect View.

Đương nhiên là không lỗi, không ngu đến nỗi đưa lên diễn đàn file lỗi làm chi.

Máy bạn có ma ám rồi! Có thể không tin mình, thì chờ người thứ ba xác nhận vậy!
 
Upvote 0
Đấy! Em chỉ chờ Anh xác nhận để Em cài lại Win và Office.
Cám Ơn Anh!
Chúc Anh ngày đầu tuần Vui Vẻ!
 
Upvote 0
Mã:
Private [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Sub UserForm_Initialize[/FONT][/COLOR][COLOR=#007700][FONT=monospace]()
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace](), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace](), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]R [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String
With Sheets[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Nhap"[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"B2"[/FONT][/COLOR][COLOR=#007700][FONT=monospace], .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"B2"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]xlDown[/FONT][/COLOR][COLOR=#007700][FONT=monospace])).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace](, [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value
    R [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ReDim dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To R[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 3[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End With
With CreateObject[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Scripting.Dictionary"[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To R
        Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) & [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Not [/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Exists[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Then
            K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]+ [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]: [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]): [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace]): [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
            .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Add Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]""
        [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next I
End With
With ListBox1
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ColumnCount [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace]: .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ColumnWidths [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"90;190;40"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]: .List = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr
End With
End Sub 
[/FONT][/COLOR]

Th
ầy Ba Tê cho em hoi tí, nếu bây giờ em chỉ cần lọc không trùng ở 2 cột thôi thì chỉnh code làm sao Ah!
và các chữ I, K, R trong sub có ý nghĩa gì ah!
và "Tem = sArr(I, 1) & sArr(I, 2)" là sao ah? có thể là "Tem = sArr(I, 1)" được không ah!
 
Upvote 0
Em chỉnh lại như vầy thì được, nhưng có gì sai, mong Thầy chỉ bảo.
Mã:
Private Sub UserForm_Initialize()
Dim sArr(), dArr(), I As Long, K As Long, R As Long, Tem As String
With Sheets("Nhap")
    sArr = .Range("C3", .Range("C3").End(xlDown)).Resize(, 2).Value
    R = UBound(sArr)
    ReDim dArr(1 To R, 1 To 2)
End With
With CreateObject("Scripting.Dictionary")
    For I = 1 To R
        Tem = sArr(I, 1) & sArr(I, 2)
        If Not .Exists(Tem) Then
            K = K + 1: dArr(K, 1) = sArr(I, 1): dArr(K, 2) = sArr(I, 2)
            .Add Tem, ""
        End If
    Next I
End With
With ListBox1
    .ColumnCount = 2: .ColumnWidths = "210;40": .List = dArr
End With
End Sub
nhưng sao em thấy cái ListBox của em kéo hết dữ liệu nó còn xuống sâu quá.
 
Upvote 0
Mã:
Private [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Sub UserForm_Initialize[/FONT][/COLOR][COLOR=#007700][FONT=monospace]()
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace](), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace](), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]R [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String
With Sheets[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Nhap"[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"B2"[/FONT][/COLOR][COLOR=#007700][FONT=monospace], .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"B2"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]xlDown[/FONT][/COLOR][COLOR=#007700][FONT=monospace])).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace](, [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value
    R [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ReDim dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To R[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 3[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End With
With CreateObject[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Scripting.Dictionary"[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To R
        Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) & [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
        If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Not [/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Exists[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Then
            K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]+ [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]: [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]): [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace]): [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
            .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Add Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]""
        [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next I
End With
With ListBox1
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ColumnCount [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]3[/FONT][/COLOR][COLOR=#007700][FONT=monospace]: .[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ColumnWidths [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"90;190;40"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]: .List = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr
End With
End Sub 
[/FONT][/COLOR]

Th
ầy Ba Tê cho em hoi tí, nếu bây giờ em chỉ cần lọc không trùng ở 2 cột thôi thì chỉnh code làm sao Ah!
và các chữ I, K, R trong sub có ý nghĩa gì ah!
và "Tem = sArr(I, 1) & sArr(I, 2)" là sao ah? có thể là "Tem = sArr(I, 1)" được không ah!

Ý bạn là sao?
- Cần lọc không trùng 2 cột là 2 cột nào, hiện code đang lọc không trùng 2 cột Nhà cung cấp và Tên hàng
- I, K, R là các biến kiểu Long đã khai báo bên trên.
- Tem khai báo là chuỗi (String), Tem = sArr(I, 1) & sArr(I, 2) để ghép 2 chuỗi Nhà cung cấp và Tên hàng thành 1 chuỗi để xét trùng.
Nếu Tem=sArr(I, 1) thì chỉ xét trùng 1 điều kiện là Nhà cung cấp.
 
Upvote 0
Cột ĐVT cũng phải xét chứ Thầy???
Xin Thầy giải thích dùm em về Lý thuyết lọc không trùng bằng Dic ah!
Giống như là nếu xét không trùng thì đầu tiên là khai báo bie6n1I.K.R, rồi Dic as Object, rồi biến tạm, lọc vào biến tạm rồi nạp vào LisBox.lis..v..v.....v
 
Upvote 0
Em chỉnh lại như vầy thì được, nhưng có gì sai, mong Thầy chỉ bảo.
Mã:
Private Sub UserForm_Initialize()
Dim sArr(), dArr(), I As Long, K As Long, R As Long, Tem As String
With Sheets("Nhap")
    sArr = .Range("C3", .Range("C3").End(xlDown)).Resize(, 2).Value
    R = UBound(sArr)
    ReDim dArr(1 To R, 1 To 2)
End With
With CreateObject("Scripting.Dictionary")
    For I = 1 To R
        Tem = sArr(I, 1) & sArr(I, 2)
        If Not .Exists(Tem) Then
            K = K + 1: dArr(K, 1) = sArr(I, 1): dArr(K, 2) = sArr(I, 2)
            .Add Tem, ""
        End If
    Next I
End With
With ListBox1
    .ColumnCount = 2: .ColumnWidths = "210;40": .List = dArr
End With
End Sub
nhưng sao em thấy cái ListBox của em kéo hết dữ liệu nó còn xuống sâu quá.

Nạp nguyên mảng dArr() vào list gồm R dòng nên có nhiều dòng trống bên dưới.
Muốn nạp chihs xác số dòng vào List chắc phải cho nó chép lại vào mảng khác.
PHP:
Private Sub UserForm_Initialize()
Dim sArr(), dArr(), Arr(), I As Long, K As Long, R As Long, Tem As String
With Sheets("Nhap")
    sArr = .Range("C2", .Range("C2").End(xlDown)).Resize(, 2).Value
    R = UBound(sArr)
    ReDim dArr(1 To R, 1 To 2)
End With
With CreateObject("Scripting.Dictionary")
    For I = 1 To R
        Tem = sArr(I, 1) & sArr(I, 2)
        If Not .Exists(Tem) Then
            K = K + 1: dArr(K, 1) = sArr(I, 1): dArr(K, 2) = sArr(I, 2)
            .Add Tem, ""
        End If
    Next I
End With
ReDim Arr(1 To K, 1 To 2)
For I = 1 To K
    Arr(I, 1) = dArr(I, 1): Arr(I, 2) = dArr(I, 2)
Next I
With ListBox1
    .ColumnCount = 2: .ColumnWidths = "190;40": .List = Arr
End With
End Sub
- Chuyện giải thích dùng biến, Dic... chắc là bạn phải tự tìm trên GPE thôi.
Ví dụ chỗ này:
http://www.giaiphapexcel.com/forum/...n-về-Scripting-Dictionary&p=374487#post374487
 
Lần chỉnh sửa cuối:
Upvote 0
Ý của em hỏi là code đầu là 3 cột chứ Thầy. Cột "Nhà Cung Cấp" và "Tên Hàng Hóa" và "ĐVT" mà sao chỉ cần nối chuỗi :
Mã:
[COLOR=#0000BB][FONT=monospace]Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) & [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR]
Còn bây giờ em chỉ cần lọc cột "Tên Hàng Hóa" và "ĐVT" cũng cần nối chuỗi
Mã:
[COLOR=#0000BB][FONT=monospace]Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) & [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR]
AH mà Thầy có thể Sort dữ liệu tromg mảng Arr để nạp vảo ListBox cho dễ tìm được không Thầy.
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom