Lọc không trùng bằng DIC (1 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 đỡ.
 
Ở 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")
1' . . . '
     i = 1 To UBound(arr)
      Dic(arr(i, 1)) = ""
    ListBox1.List=arr
    Set Dic = Nothing
  End If
End Sub

1./ " Tất nhiên là không trùng": Dữ liệu bạn trãi dài trên 3 cột; Vậy bạn muốn không trùng ở cột nào? (Đừng nói là không trùng ở cả 3 cột à nha!)

2./ Bạn chạy thử cái í tưởng của bạn chưa & quan trọng hơn kết quả thu được có như ước nguyện của bạn không? (& đừng nói là bạn chưa thử í tưởng của mình trên thực địa à nha!)

3./ Nếu (2) vẫn chưa thỏa thì có lẽ bạn còn cần thêm vài dòng lệnh để không nạp hết những dòng trùng vô các 'Dic' thân thương của bạn. (chuyện này có thể bạn tìm tiếp trên diễn đàn sẽ có)

Chúc vui & nhiều kết quả đến với bạn!__--__
 
Upvote 0
Không trùng ở cột B bạn ah!
Mong được sự giúp đỡ.
 
Upvote 0
Vậy bạn tham khảo Code này, của ai đó trên diễn đàn (Cách đây gần 5 năm)
Mã:
Sub Cau1()
Dim Dict As Object, J As Long, W As Long
Dim Arr() As Variant, sArr As Variant
With Sheets("Cau1")
  .Range("J4").CurrentRegion.Offset(1).ClearContents
  Set Dict = CreateObject("Scripting.Dictionary")
    sArr = Sheet1.Range("b2:g21").Value
    ReDim Arr(1 To UBound(sArr, 1), 1 To 6)
    For J = 1 To UBound(sArr, 1)
        If Not IsEmpty(sArr(J, 2)) And Not Dict.exists(sArr(J, 2)) Then
            W = W + 1
             Dict.Add sArr(J, 2), W
             Arr(W, 1) = sArr(J, 1)
             Arr(W, 2) = sArr(J, 2)
            If sArr(J, 3) <> "" Then
                Arr(W, 3) = sArr(J, 6)
            Else
                Arr(W, 4) = sArr(J, 6)
            End If
        Else
            If sArr(J, 3) <> "" Then
                Arr(Dict.Item(sArr(J, 2)), 3) = Arr(Dict.Item(sArr(J, 2)), 3) + sArr(J, 6)
                MsgBox Dict.Item(sArr(J, 2))
            Else
                Arr(Dict.Item(sArr(J, 2)), 4) = Arr(Dict.Item(sArr(J, 2)), 4) + sArr(J, 6)
            End If
        End If
    Next J
.Range("j4").Resize(W, 4).Value = Arr
End With

End Sub
 
Upvote 0
Bạn giúp mình code nạp luôn vào ListBox1 nhe.
Cám ơn Bạn nhiều.
 
Upvote 0
Mong mọi người giúp em với!
 
Upvote 0
Em xin đưa file mong mọi người giúp lọc không trùng đưa vào ListBox1, trong file em nạp hết dữ liệu "B2:D" của Sheet"Nhap" vào listBox1.
Bây giờ em mong mọi người giúp em nạp không trùng vào ListBox1
Hiện ListBox1 đang trùng 2 (Chi Thơm- Đường Tinh Luyện) và 2 (Anh Tuấn - Bánh Bao Loại To)
Em muốn là nếu trùng Nhà Cung cấp và Tên hàng Hóa thì loc không trùng.
Click chọn "B2" sheet"Xuat" sẽ hiện Form.
Em Cám Ơn mọi người.
 

File đính kèm

Lần chỉnh sửa cuối:
Upvote 0
Mong mọi người giúp em với!
 
Upvote 0
PHP:
Private Sub UserForm_Initialize()
 Dim lRow As Long, J As Long, W As Long, Col As Byte
 Dim Arr(), Dict As Object:             Dim Tmp As String
 
 With ListBox1
    .ColumnCount = 3:                   .ColumnWidths = "90;190;40"
 End With
 lRow = Sheet1.Cells(Rows.count, "B").End(xlUp).Row
'Chi Doc Du Lieu Vào Mang Arr() Khi Có Du Lieu (lRow > 1)'
 If lRow > 1 Then
    Arr = Sheet1.Range("B2:D" & lRow).Value
    ReDim dArr(1 To UBound(Arr()), 1 To 3)
    Set Dict = CreateObject("Scripting.Dictionary")
    For J = 1 To UBound(Arr, 1)
        Tmp = Arr(J, 1) & Arr(J, 2)
        If Not IsEmpty(Arr(J, 1)) And Not Dict.exists(Tmp) Then
            W = W + 1
            Dict.Add Tmp, W
            For Col = 1 To 3
                dArr(W, Col) = Arr(J, Col)
            Next Col
        Else
        End If
    Next J
    ListBox1.List = dArr()
 Else
    MsgBox "Không Có Du Lieu", , "GPE.COM Xin Chào!"
 End If
End Sub
 
Upvote 0
Cám Ơn Anh Hoang2013 nhiều nhiều!
 
Upvote 0
Sao em Copy Code Anh vào thì bị lỗi : "Coompile Error" " Expected:= "
Mong Anh giúp.
 
Upvote 0
Sao trong Code của Anh có 6 dòng chữ đỏ.
Còn của em tới 12 dòng chữ đỏ lựn.
 
Upvote 0
Mong mọi người giúp đỡ!
 
Upvote 0
Có phải câu lỗi ở bài 11 là thiếu dấu bằng ( =) mà em chả biết thêm vào đâu?
 
Lần chỉnh sửa cuối:
Upvote 0
Sao em thấy đã có 2 Anh hkhuong và winvista xem mà không trả lời dùm em vậy???
 
Upvote 0
Có phải câu lỗi ở bài 11 là thiếu dấu bằng ( =) mà em chả biết thêm vào đâu?

Bạn xài thử code này coi sao:
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)
End With
End Sub
'------------------------------------------------------------------------'
Private Sub UserForm_Initialize()
Dim sArr(), dArr(), I As Long, K As Long, R As Long, Tem As String
With Sheets("Nhap")
    sArr = .Range("B2", .Range("B2").End(xlDown)).Resize(, 3).Value
    R = UBound(sArr)
    ReDim dArr(1 To R, 1 To 3)
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): dArr(K, 3) = sArr(I, 3)
            .Add Tem, ""
        End If
    Next I
End With
With ListBox1
    .ColumnCount = 3: .ColumnWidths = "90;190;40": .List = dArr
End With
End Sub
 
Upvote 0
Sao ngay cả Code của Thầy Ba Tê cũng bị lỗi.
Em xin Anh Hoang2013 và Thầy Ba Tê ghi Code thẳng vào file của em đưa lên rồi đính kèm lên DD dùm em ah.
Em nghi ngờ máy em có vấn đề????
 
Upvote 0
Sao ngay cả Code của Thầy Ba Tê cũng bị lỗi.
Em xin Anh Hoang2013 và Thầy Ba Tê ghi Code thẳng vào file của em đưa lên rồi đính kèm lên DD dùm em ah.
Em nghi ngờ máy em có vấn đề????

Nó đây................................................................
 

File đính kèm

Upvote 0
File Thầy Ba Tê gửi lên không lỗi.
Sao em copy code của Thầy và Anh Hoang2013 vào File gốc em gửi lên lại thì lỗi.
Kỳ vậy ta.
 
Upvote 0
hay do lỗi PHP Code
Mã:
[COLOR=#007700][FONT=Courier New]Private [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Sub UserForm_Initialize[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]()
 [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Dim lRow [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Long[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]J [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Long[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]W [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Long[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Col [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Byte
 Dim Arr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New](), [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Dict [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Object[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]:             [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Dim Tmp [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]String
 
 With ListBox1
    [/FONT][/COLOR][COLOR=#007700][FONT=Courier New].[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]ColumnCount [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]3[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]:                   .[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]ColumnWidths [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#DD0000][FONT=Courier New]"90;190;40"
 [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]End With
 lRow [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Sheet1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New].[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Cells[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Rows[/FONT][/COLOR][COLOR=#007700][FONT=Courier New].[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]count[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#DD0000][FONT=Courier New]"B"[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]).[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]End[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]xlUp[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]).[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Row
[/FONT][/COLOR][COLOR=#DD0000][FONT=Courier New]'Chi Doc Du Lieu Vào Mang Arr() Khi Có Du Lieu (lRow > 1)'
 [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]If [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]lRow [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]> [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1 Then
    Arr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Sheet1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New].[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Range[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#DD0000][FONT=Courier New]"B2:D" [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]lRow[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]).[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Value
    ReDim dArr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Arr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]()), [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1 To 3[/FONT][/COLOR][COLOR=#007700][FONT=Courier New])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Set Dict [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]CreateObject[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#DD0000][FONT=Courier New]"Scripting.Dictionary"[/FONT][/COLOR][COLOR=#007700][FONT=Courier New])
    For [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]J [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Arr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New])
        [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Tmp [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Arr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]J[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) & [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Arr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]J[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]2[/FONT][/COLOR][COLOR=#007700][FONT=Courier New])
        If [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Not IsEmpty[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Arr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]J[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New])) And [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Not Dict[/FONT][/COLOR][COLOR=#007700][FONT=Courier New].[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]exists[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Tmp[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Then
            W [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]W [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]+ [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1
            Dict[/FONT][/COLOR][COLOR=#007700][FONT=Courier New].[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Add Tmp[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]W
            [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]For [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Col [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]1 To 3
                dArr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]W[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Col[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Arr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]J[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Col[/FONT][/COLOR][COLOR=#007700][FONT=Courier New])
            [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Next Col
        [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]Else
        [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]End [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]If
    [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]Next J
    ListBox1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New].List = [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]dArr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]()
 Else
    [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]MsgBox [/FONT][/COLOR][COLOR=#DD0000][FONT=Courier New]"Không Có Du Lieu"[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], , [/FONT][/COLOR][COLOR=#DD0000][FONT=Courier New]"GPE.COM Xin Chào!"
 [/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]End [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]If
[/FONT][/COLOR][COLOR=#0000BB][FONT=Courier New]End Sub  [/FONT]
[/COLOR]
 
Upvote 0
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

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

Back
Top Bottom