AutoFilter last 2 number with wildcard

HelloKhritty

New Member
Joined
Jan 7, 2022
Messages
9
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
Platform
  1. Windows
Hi,
I'm working on an autofilter in VBA and having a hard time in executing the code. so my problem is, I'd like to get all the number that has the last 2 digits same as on a cell value. for example the cell contains 51. I'd like to get 451, 551, 751,651 all that has the number 51 in it. the code was successful when i am only using the code without the wildcard. but when i am adding the asterisk, the cells are emptied when i clicked the command button. I will appreciate your help a lot. Thank you.

Sub team()
Dim val As String
val = "*" & Range("B3").Value & "*"

With Worksheets("Sheet1").Range("A7")
.AutoFilter Field:=3, Criteria1:=val, Operator:=xlFilterValues
End With

End Sub

1649966783109.png
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
One way to filter by multiple values is with an array, the values in the array should be stored as text.

Try this:

VBA Code:
Sub Filter_Team()
  Dim a() As Variant
  Dim i As Long
  Dim r As Range, f As Range
  Dim cell As String, sTeam As String
  
  Application.ScreenUpdating = False
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  sTeam = Range("B3").Value
  Set r = Range("C:C")
  Set f = r.Find(sTeam, , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      If Right(f.Value, Len(sTeam)) = sTeam Then
        i = i + 1
        ReDim Preserve a(1 To i)
        a(i) = f.Text
      End If
      Set f = r.FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If

  If i > 0 Then Range("A7").AutoFilter 3, Array(a), xlFilterValues
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Could you use Advanced Filter instead?

Place the formula in C3 as shown then run the code below.
(You can use any other cell than C3 provide that the cell immediately above it is blank and you adjust the code to reflect the changed location.)

My original sample data

HelloKhritty.xlsm
ABCDEFGHIJ
1
2
3Team46FALSE
4
5
6
7Hdr1Hdr2TEAMHdr4Hdr5Hdr6Hdr7Hdr8Hdr9Hdr10
8datadata200datadatadatadatadatadatadata
9datadata246datadatadatadatadatadatadata
10datadata5datadatadatadatadatadatadata
11datadata46datadatadatadatadatadatadata
12datadata2569datadatadatadatadatadatadata
13datadata58datadatadatadatadatadatadata
14datadata946datadatadatadatadatadatadata
15datadata101datadatadatadatadatadatadata
16datadata102datadatadatadatadatadatadata
17datadata103datadatadatadatadatadatadata
18datadata104datadatadatadatadatadatadata
Sheet1
Cell Formulas
RangeFormula
C3C3=RIGHT(C8,2)-B$3=0


VBA Code:
Sub FilterTeam()
  Range("A7").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("C2:C3"), Unique:=False
End Sub

My sheet after running the code

HelloKhritty.xlsm
ABCDEFGHIJ
1
2
3Team46FALSE
4
5
6
7Hdr1Hdr2TEAMHdr4Hdr5Hdr6Hdr7Hdr8Hdr9Hdr10
9datadata246datadatadatadatadatadatadata
11datadata46datadatadatadatadatadatadata
14datadata946datadatadatadatadatadatadata
19
Sheet1
Cell Formulas
RangeFormula
C3C3=RIGHT(C8,2)-B$3=0
 
Upvote 0
Both works well,
Good News! But just to be sure that you are getting what you want ..

You originally said
I'd like to get all the number that has the last 2 digits same as on a cell value. for example the cell contains 51. I'd like to get 451, 551, 751,651
However, the code that you had tried was putting an asterisk before and after the B3 number. If that code had worked, then as well as the numbers listed above, you would also have got 511, 2518 etc


Also, if it happens that no teams at all end with the digits in B3 (if that is even possible) then my code will show no teams whereas Dante's code will show all teams. Either way, is that an issue for you?


If you have gone with the AutoFilter method and are happy with the "all teams" result mentioned above then you may also want to consider this adaptation of it which grabs the relevant values a little more directly/efficiently.

VBA Code:
Sub Filter_Team_v2()
  Dim a() As Variant
  Dim i As Long
  Dim r As Range, f As Range
  Dim cell As String, sTeam As String
 
  Application.ScreenUpdating = False
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  sTeam = "*" & Range("B3").Value
  Set r = Range("C:C")
  Set f = r.Find(sTeam, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      i = i + 1
      ReDim Preserve a(1 To i)
      a(i) = f.Text
      Set f = r.FindNext(f)
    Loop While f.Address <> cell
  End If
  If i > 0 Then Range("A7").AutoFilter 3, Array(a), xlFilterValues
  Application.ScreenUpdating = True
End Sub

If you have gone with Advanced Filter and are happy with the "no teams" result mentioned above and it is possible that the B3 value could be less or more than 2 digits, then just change the C3 formula to
Excel Formula:
=RIGHT(C8,LEN(B3))-B$3=0
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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