Combining two vba codes?

Boniouk

Board Regular
Joined
Aug 2, 2013
Messages
166
I have to private sub worksheet changes.
One is to filter data by cell C2 and the other is to filter by cell F2. Each looks to filter via different columns. I got these codes elsewhere, does anyone know how to run them both as its giving me an error when searching from cell F2. It highlights the 2nd private Sub statement.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If (Intersect(Target, Range("C2")) Is Nothing) _
   Then
      Exit Sub
   End If
   Cells.AutoFilter Field:=4, Criteria1:="=" & Range("C2")
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
   If (Intersect(Target, Range("F2")) Is Nothing) _
   Then
      Exit Sub
   End If
   Cells.AutoFilter Field:=13, Criteria1:="=" & Range("F2")
End Sub
 
Last edited by a moderator:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this (you can only have one Change event):
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("C2")) Is Nothing Then
     Cells.AutoFilter Field:=4, Criteria1:="=" & Range("C2").Value
   ElseIf Not Intersect(Target, Range("F2")) Is Nothing Then 
     Cells.AutoFilter Field:=13, Criteria1:="=" & Range("F2").value
   End If
End Sub
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C2")) Is Nothing Then
    Cells.AutoFilter Field:=4, Criteria1:="=" & Range("C2")
ElseIf Not Intersect(Target, Range("F2")) Is Nothing Then
    Cells.AutoFilter Field:=13, Criteria1:="=" & Range("F2")
End If
End Sub
 
Upvote 0
Thankyou very much to both. Works excellently.
Guys, do either of you know, my technical skills with excel is quite advanced using excel/formulas, but rather basic with VBA. Does this site contain and starter guides or lessons for vba? Would love to be able to help out more here and ask less.
 
Upvote 0

Forum statistics

Threads
1,203,140
Messages
6,053,727
Members
444,681
Latest member
Nadzri Hassan

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