Autofilter using VBA code getting error with condition in different cell than table?

babaso_tawase

Board Regular
Joined
Feb 5, 2017
Messages
73
Office Version
  1. 2007
Platform
  1. Windows
I have table data like below of range a8:q20 and condition in cell E2 with combobox with dropdown value 1 to 12
Sheet name is linearity and table name is points

I used vba code

Private Sub ComboBox1_Change()
Sheet1.Unprotect Password:="a"
Dim points As Range
With Worksheets("Linearity")
Set points = .Range("E2")
With .Range("a8:q20")
.AutoFilter Field:=1, Criteria1:="<=" & points, Operator:=xlFilterValues, Visibledropdown:=False
End With
End With
Sheet1.Protect Password:="a"
End Sub


but getting error whole running code with option end or debug.
Please help in code.

Sr.noData1Data2Data3Data4
1abcW3teTy
2adeAt
3aaAtr
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try changing the Dim points to string
Then remove the Set and just have points = .range("E2").Value
See if that works.
 
Upvote 0
Thanks alot.

I used below vba code

Private Sub ComboBox1_Change()
Sheet1.Unprotect Password:="a"

Dim points As Range
With Worksheets("Linearity")
Set points = .Range("E14")
End With

With Worksheets("Linearity").Range("A18:s37")
.AutoFilter Field:=1, Criteria1:="<=" & CStr(points), Operator:=xlFilterValues, Visibledropdown:=False
End With

Sheet1.Protect Password:="a"

End Sub


But getting runtime error 1004 autofilter method of range class failed. Please help
 
Upvote 0
and table name is points
What is the range of the table "points" ?
Are you trying to filter the table points or trying to use the data in points to filter the other data in which case is the other data also a table and if so what is its name ?
Maybe provide a screen shot showing row and column references of the points table and if different the other table.
What is this line trying to do Set points = .Range("E14") ?
PS: you changed the range in Post 3, which is correct ?
 
Upvote 0
Thank for reply.

I here attached screen shot.

Here data changed , reference cell criteria in E2 as combo box dropdown linked cell. I just want filter and show number of rows equal to number in E2 cell. Suppose in combo box I select 7 then show rows upto 15 i.e corresponding up to number 7 in column A and hide row 16 to 20 mean number in column A is 8 to 12.
Thanks in advance.
 

Attachments

  • Linearity sceen.png
    Linearity sceen.png
    83.1 KB · Views: 9
Upvote 0
Give this a try:

VBA Code:
Private Sub ComboBox1_Change()
Sheet1.Unprotect Password:="a"

Dim critPoints As Long
Dim tblPoints As ListObject

With Worksheets("Linearity")
    critPoints = .Range("E2").Value
End With

Set tblPoints = Range("Points").ListObject

With tblPoints
    If tblPoints.ShowAutoFilter = True Then
        tblPoints.AutoFilter.ShowAllData
    End If
    
    .Range.AutoFilter Field:=1, Criteria1:="<=" & critPoints, Operator:=xlFilterValues ', Visibledropdown:=False
End With

Sheet1.Protect Password:="a"

End Sub
 
Upvote 0
Sheet1.Unprotect Password:="a" Dim critPoints As Long Dim tblPoints As ListObject With Worksheets("Linearity") critPoints = .Range("E2").Value End With Set tblPoints = Range("Points").ListObject With tblPoints If tblPoints.ShowAutoFilter = True Then tblPoints.AutoFilter.ShowAllData End If .Range.AutoFilter Field:=1, Criteria1:="<=" & critPoints, Operator:=xlFilterValues ', Visibledropdown:=False End With Sheet1.Protect Password:="a"
Thanks for reply.

Working fine but error as you can not this command in protected sheet. Please suggest any correction.


Mean while I used below code but same error
Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim filterValue As Long

Set ws = ThisWorkbook.Sheets("Linearity") ' Change "Sheet1" to your actual sheet name

filterValue = ws.Range("E2").Value

If ws.AutoFilterMode Then
ws.AutoFilterMode = False
End If

With ws.Range("A9:Q20")
.AutoFilter Field:=1, Criteria1:="<=" & filterValue
End With
End Sub



But same error
 

Attachments

  • error.png
    error.png
    127.9 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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