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
Web KT

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

Back
Top Bottom