Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

excel - Textbox to filter all values that matches in range

I have been using below VBA code to filter the data by typing starting words in textBox and it works great.

The issue i have been facing is when i type any numeric value and alpha middle, end or any other value that is available in filter range it does not work.

for example i have data in Col"C" that is

King
Tomb
River
Ocean
8899
1011
4568

I will type "Ki" or "To" or "Ri" starting words/value code will work.

when i type midlle, last or numeric value it will not work such as "mb", "ver", "ean" or any numeric value "4568", "11".

There could be any solution which can work this way.

You help will be appreciated.

   Private Sub TextBox2_Change()
    Dim textb_deger As String, bul As Range
    Dim start As Long
    On Error Resume Next
    textb_deger = TextBox2.Value
start = Sheets("AutoFilter").Cells(Rows.Count, 3).End(xlUp).Row
Set bul = Range("C4:C" & start).Find(What:=textb_deger)
Application.Goto Reference:=Range(bul.Address), Scroll:=False
Selection.AutoFilter field:=3, Criteria1:=textb_deger & "*"
If textb_deger = "" Then
Selection.AutoFilter
ActiveWindow.ScrollRow = 1
End If
Set bul = Nothing
End Sub
 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It appears that your data is a mixture of numeric values and text values. In order to implement filtering that Contains the sub-string 11 rather than BeginsWith 11, all the data must be Text. (whether the data looks like a number or not).

Once that has been accomplished, you can use something like:

Criteria1:="=*" & textb_deger & "*"

For example:

enter image description here

All the data in this example is Text. Running this code:

Sub qwerty()
    Dim s As Worksheet, textb_deger As String
    
    Set s = ActiveSheet
    
    textb_deger = "11"
    
    s.AutoFilterMode = False
    s.Range("A:A").AutoFilter
    s.Range("A:A").AutoFilter field:=1, Criteria1:="=*" & textb_deger & "*"
End Sub
    

Produces:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...