[Help] Search trong Filter (2 người xem)

  • Thread starter Thread starter phutc90
  • Ngày gửi Ngày gửi
Liên hệ QC

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

phutc90

Thành viên mới
Tham gia
8/1/14
Bài viết
3
Được thích
1
Chào mọi người,

Mình muốn nhờ mọi người giúp về vấn đề trong Filter.

Điều em muốn làm nó tương tự như hình, tính năng này có tên excel 2013, nhưng phiên bản dưới thì không có.

Em muốn làm cái textbox tương tự như SEARCH như hình để mọi phiên bản excel điều sử dụng được.

Mong mọi người giúp đở



1.jpg2.jpg


Thanks and best regards,
 

File đính kèm

Cái Search đó thì 2010 cũng có nha bạn, còn bạn muốn thì phải dùng đến lập trình mới có thể lọc được như vậy thôi.
 
Em có đính kèm file, anh có thể demo giúp em với .

em cũng có search GG về macro nhưng ko hiểu lắm

Thanks anh

Khi mở File, nhớ bấm vào nút Enable Content nha bạn!

attachment.php



Trên sheet, tôi tạo một ComboBox (ẩn trên dòng tiêu đề)

Tôi tạo sự kiện SelectionChange cho Sheet đó:

Mã:
Private pri_ArrFilter()
Private pri_RangeFilter As Range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Rows.Count > 1 Or Target.Columns.Count > 1 Then Exit Sub
    Dim SearchRange As Range
    Set SearchRange = Intersect(Target, Range("A4:F4"))
    If SearchRange Is Nothing Then
        ComboBox1.Visible = False
    Else
        ActiveSheet.AutoFilterMode = False
        Dim LastRow As Long, c As Long
        c = Target.Column
        LastRow = Cells(Rows.Count, c).End(xlUp).Row
        If LastRow <= 5 Then
            ComboBox1.Visible = False
            Exit Sub
        End If


        Dim n As Long, r As Long
        Dim Collect As New Collection
        Dim ArrFilter, Tmp, GetKeys()


        Set pri_RangeFilter = Range(Cells(5, c), Cells(LastRow, c))


        ArrFilter = Range(Cells(6, c), Cells(LastRow, c))


        If Not IsArray(ArrFilter) Then ArrFilter = Array(ArrFilter)


        'Loc duy nhat:
        For r = 1 To UBound(ArrFilter)
            Tmp = ArrFilter(r, 1)
            If Tmp > "" And Not Exists(Collect, Tmp) Then
                Collect.Add Empty, Tmp
                n = n + 1
                ReDim Preserve GetKeys(1 To n)
                GetKeys(n) = r
            End If
        Next


        ReDim pri_ArrFilter(1 To n, 1 To 1)
        For r = 1 To n
            pri_ArrFilter(r, 1) = ArrFilter(GetKeys(r), 1)
        Next


        With ComboBox1
            .Visible = False
            .Text = ""
            .List = pri_ArrFilter
            .Top = Target.Top
            .Left = Target.Left
            .Width = Target.Width
            .Height = Target.Height
            .Visible = True
            .Activate
        End With
    End If
End Sub

Sự kiện này, tôi lọc duy nhất các mục trong mỗi cột mà bạn chọn vào tiêu đề, sau đó gán mảng lọc duy nhất đó vào trong ComboBox.

Và sự kiện của ComboBox như sau:

Mã:
Private Sub ComboBox1_GotFocus()
    ComboBox1.DropDown
End Sub


Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
    Case 13
        If ComboBox1.MatchFound Then
            ComboBox1.Visible = False
            pri_RangeFilter.AutoFilter 1, ComboBox1, , , False
            ActiveCell.Offset(1).Select
        End If
    Case 9, 37 To 40
    Case Else
        If ComboBox1 > "" Then ComboBox1.DropDown
        Dim ArrFilter()
        Dim strType As String
        Dim n As Long, r As Long
        
        strType = "*" & UCase(ComboBox1) & "*"
        
        For r = 1 To UBound(pri_ArrFilter)
            If UCase(pri_ArrFilter(r, 1)) Like strType Then
                n = n + 1
                ReDim Preserve ArrFilter(1 To n)
                ArrFilter(n) = pri_ArrFilter(r, 1)
            End If
        Next
        If n Then
            ComboBox1.List = ArrFilter
        Else
            ComboBox1.List = Array()
        End If
    End Select
End Sub

Khi bạn gõ vào trong ComboBox này, thì chúng sẽ lọc những gì giống hoặc na ná những thứ bạn gõ, đồng thời xổ ra một List để bạn chọn lựa mục nào bạn muốn, sau đó bạn bấm Enter nó sẽ Filter cho bạn theo mục mà bạn chọn vào.
 

File đính kèm

  • SearchFilter.xlsm
    SearchFilter.xlsm
    41.3 KB · Đọc: 54
  • EnableContents.jpg
    EnableContents.jpg
    82.4 KB · Đọc: 77
Cảm ơn anh Hoàng Trọng Nghĩa rất nhiều, mặc dù chưa hiểu lắm, nhưng sẽ cố gắng nghiên cứu thêm
 
Web KT

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

Back
Top Bottom