Filter with cell value using VBA

rsjury

New Member
Joined
Jun 28, 2014
Messages
48
I have create the following Code in a worksheet (automatically runs when data is entered into B4) that works with two issues. I want to add a third IF statement and I get a runtime error when I enter information in another cell other than in "B4". the two "Calls" are for filtering a list based on what is selected from a data validation list in "B4".

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Intersect(Target, Range("B4"))
Application.EnableEvents = False
If r = "All Services" Then
Call Test3
Else
Call Test2
End If
Application.EnableEvents = True
End Sub

Again, just want to add a third if with another call and be able to enter data in other cells without a runtime error.

THANKS!

BTW, My Calls

Sub Test2()
Dim C As Range
Set C = Range("B4")
ActiveSheet.Range("$B$5:$B$1000").AutoFilter Field:=1, Criteria1:=C.Value
End Sub

Sub Test3()
Selection.AutoFilter
End Sub
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try adding this at the beginning of your Worksheet_Change event procedure:

If Target.Address <> "$B$4" Then Exit Sub
 
Upvote 0
Cool, Thanks! That works.. How about the other issue, I tried adding elseif but it would not work?
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

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