To perform same task on worksheet (1), (2) and (3), not worksheet (4),(5) and (6)

Mangosteenlu

New Member
Joined
Jul 5, 2015
Messages
12
Hi All,
I only know a little about VBA, so can someone please help me to amend the below VBA code to perform the same task in worksheets (1), (2), and (3), not in worksheet (4), (5) and (6)?

Basically I just want to automatically filter column A with criteria of 'Overview' for worksheets (1) (2) and (3), the rest worksheet (4), (5), and (6) does not perform this task. so i wrote a simple VBA code 1 below, but it only works for the worksheet (1), not others. Also i need to take off the autofilter once the task is performed. (code 2 below does not seem to work) . Thank you for your help.

1. Sub Auto_Filter_with_Criteria_Overview ()
Range("A11:A182").AutoFilter field:=1, Criteria1:="Overview"
End Sub

2. Sub Take_off_Auto_Filter ()
Range("A11:A182").AutoFilterMode=false
 

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,)
Hi, you could give this a try:

VBA Code:
Sub Auto_Filter_with_Criteria_Overview()
Dim i As Long
For i = 1 To 3
    Worksheets(i).Range("A11:A182").AutoFilter field:=1, Criteria1:="Overview"
Next i
End Sub

VBA Code:
Sub Take_off_Auto_Filter()
Dim i As Long
For i = 1 To 3
    Worksheets(i).AutoFilterMode = False
Next i
End Sub
 
Upvote 0
thank you very much for the reply, yes it works.

but it does not work after I have changed worksheet name from sheet 1 to 'small tools' , from Sheet 2 to 'survey' and from sheet 3 to 'formwork' ,

Can you shine some light on this please?
 
Upvote 0
When you're using the index, it references the position of the sheet e.g. Worksheet(1) is the leftmost sheet. If you keep the same position and change the name, it would be fine, but it's still risky. Rather than using the index, use the actual name instead (even better if we use the code name instead of the display name). Note I've added error handling in case your sheet doesn't exist anymore. Not tested. Try on a copy.

VBA Code:
Sub Auto_Filter_with_Criteria_Overview()
    Dim wsNames As Variant
    Dim wsName As Variant
    Dim ws As Worksheet
  
    wsNames = Array("small tools", "survey", "formwork")
  
    For Each wsName In wsNames
        On Error Resume Next
        Set ws = ThisWorkbook.Worksheets(wsName)
        On Error GoTo 0
      
        If Not ws Is Nothing Then
            ws.Range("A11:A182").AutoFilter Field:=1, Criteria1:="Overview"
        Else
            GoTo NextWorksheet
        End If
NextWorksheet:
    Next wsName
End Sub

VBA Code:
Sub Take_off_Auto_Filter()
    Dim wsNames As Variant
    Dim wsName As Variant
    Dim ws As Worksheet
   
    wsNames = Array("small tools", "survey", "formwork")
   
    For Each wsName In wsNames
        On Error Resume Next
        Set ws = ThisWorkbook.Worksheets(wsName)
        On Error GoTo 0
       
        If Not ws Is Nothing Then
            ws.AutoFilterMode = False
        End If
    Next wsName
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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