Unable to filter data using vba for numbers

R Kunal

New Member
Joined
Jul 26, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
I am having table with Column 1 - S.No., Column 2 - Adm. No (Example : 7898) , Column 3 - Name (Example : Jack Samuel) , Column 4 - Mobile No.(Example : 9898989898)
I Set Three TextBox
TextBox1 = Adm. No., TextBox2 = Name, TextBox3 = Mobile No.

For Auto Filter By Name, following are the codes:

Private Sub TextBox2_Change()
TextBox1.Text = ""
TextBox3.Text = ""
Application.ScreenUpdating = False
ActiveSheet.ListObjects("SevenA_T").Range.AutoFilter Field:=4, Criteria1:="*" & [G4] & "*", Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub


With this code working filter for names,

Private Sub TextBox1_Change()
TextBox1.Text = ""
TextBox2.Text = ""
Application.ScreenUpdating = False
ActiveSheet.ListObjects("SevenA_T").Range.AutoFilter Field:=2, Criteria1:="False", Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub

It is not working with numbers (Adm. No.)
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I am having table with Column 1 - S.No., Column 2 - Adm. No (Example : 7898) , Column 3 - Name (Example : Jack Samuel) , Column 4 - Mobile No.(Example : 9898989898)
I Set Three TextBox
TextBox1 = Adm. No., TextBox2 = Name, TextBox3 = Mobile No.

For Auto Filter By Name, following are the codes:

Private Sub TextBox2_Change()
TextBox1.Text = ""
TextBox3.Text = ""
Application.ScreenUpdating = False
ActiveSheet.ListObjects("SevenA_T").Range.AutoFilter Field:=4, Criteria1:="*" & [G4] & "*", Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub


With this code working filter for names,

Private Sub TextBox1_Change()
TextBox1.Text = ""
TextBox2.Text = ""
Application.ScreenUpdating = False
ActiveSheet.ListObjects("SevenA_T").Range.AutoFilter Field:=2, Criteria1:="False", Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub

It is not working with numbers (Adm. No.)
--set the range of that column of table (number to text by custom formatting)

--Suppose we want to filter data from column 2 = field 2
-- Data starts from B9 (excluded column header)
-- Let Table's Name - SevenA_T
Add the following code
'TextBox1.Font.Size = 14
Dim xStr, xName As String
Dim xWS As Worksheet
Dim xRg As Range
Dim LR As Long, T As Long
On Error GoTo Err01
Application.ScreenUpdating = False
xName = "SevenA_T"
xStr = TextBox1.Text
Set xWS = ActiveSheet
Set xRg = xWS.ListObjects(xName).Range

' convert to text
LR = Range("B" & Rows.Count).End(xlUp).Row
For T = 9 To LR
Range("B" & T).Value = Format(Range("B" & T), "#")
Next T

If xStr <> "" Then
xRg.AutoFilter field:=2, Criteria1:="*" & xStr & "*", Operator:=xlFilterValues
Else
xRg.AutoFilter field:=2, Operator:=xlFilterValues
End If
'convert to number
For T = 9 To LR
Range("B" & T).Value = Val(Range("B" & T))
Next T

Err01:
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,524
Messages
6,114,117
Members
448,549
Latest member
brianhfield

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