vba filter exact match

amelia123456

New Member
Joined
Apr 15, 2019
Messages
8
I have a few locations, eg. AMK, AMK MRT.

When i type AMK, both amk and amk mrt will appear, how do i filter to exact when i type amk, only amk will appear?



this is my code

Sub Advanced_Filtering()


Range("A8:H1611").AdvancedFilter _
Action:=xlFilterInPlace, _
CriteriaRange:=Range("A1:C2")


End Sub





[TABLE="width: 500"]
<tbody>[TR]
[TD]code[/TD]
[TD]assets[/TD]
[TD]location[/TD]
[TD] filter(this is a button)[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]AMK[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Code[/TD]
[TD]date[/TD]
[TD]cost[/TD]
[TD]assets[/TD]
[TD]owner[/TD]
[TD]condition[/TD]
[TD]location[/TD]
[/TR]
[TR]
[TD]12345[/TD]
[TD]12/02/2016[/TD]
[TD]2000[/TD]
[TD]sealing machine[/TD]
[TD]abc[/TD]
[TD]good[/TD]
[TD]Amk[/TD]
[/TR]
[TR]
[TD]22456[/TD]
[TD]10/05/2017[/TD]
[TD]1000[/TD]
[TD]sugar machine[/TD]
[TD]abc[/TD]
[TD]good[/TD]
[TD]Amk Mrt[/TD]
[/TR]
[TR]
[TD]55624[/TD]
[TD]11/04/2018[/TD]
[TD]1800[/TD]
[TD]induction cooker[/TD]
[TD]abc[/TD]
[TD]good[/TD]
[TD]AMK[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Upvote 0
Try

Code:
Sub Advanced_Filtering()
    Dim strCriteria As String
      
    strCriteria = "AMK"
    Range("C2") = "=""=" & strCriteria & """"
        
    If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
    
    Range("A8").CurrentRegion.AdvancedFilter _
        Action:=xlFilterInPlace, CriteriaRange:=Range("A1:C2")
End Sub

M.
 
Upvote 0
Please do the following:
- Change the format of cell C2 to general format. (This is very important)
- Write in the cell C2 the Amk data without quotes or spaces, only the Amk data


That should look like this:

a833773ce982995d29d8cedf66cc4214.jpg




- Execute the following code

Code:
Sub Advanced_Filtering()
    If InStr(1, Range("C2").Value, "=") = 0 Then
        Range("C2").Formula = "=""=" & Range("C2").Value & """"
    End If
    Range("A8:H1611").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A1:C2")
End Sub
 
Upvote 0
Hi Dave,

it does not work, whenever i click on the filter button, "" will appear continuously and results will not appear.

Hi,
sorry about that, my bad should have thought about blank entry

try this update which includes the other filter criteria headings you have in the range

Code:
Sub Advanced_Filtering()
    Dim strCriteria As String
    Dim cell As Range
    
    For Each cell In Range("A2:C2")
        If Len(cell.Text) > 0 Then
        strCriteria = cell.Text
'ensure = sign not already included
        strCriteria = Replace(strCriteria, "=", "")
'add = sign to formula
        cell.Formula = "=""=" & strCriteria & """"
        End If
    Next cell
'reset filter
    If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
'apply filter
    Range("A8").CurrentRegion.AdvancedFilter _
    Action:=xlFilterInPlace, _
    CriteriaRange:=Range("A1:C2")
End Sub

Hope helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top