Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

VBA code to run on multiple sheets.

Avatar of Nila 02
Nila 02 asked on
Microsoft OfficeMicrosoft ExcelVBA
3 Comments1 Solution206 ViewsLast Modified:
Hi, I have a VBA/macro code here which autofilters the values in activesheet. My aim is to actually make the VBA code run for the sheets i specify.

Aim: When i press the form button it should filter the values in both the sheets and not just active sheet.

The methods i tried are not working, it would be really helpful if someone could help. Here is the code i have been using.

Sheets i want it to run on : "Prev Month" and "Current Month"

Sub Macro1()
' Macro1 Macro

Dim xWs As Worksheet
 Dim rng As Range, res As Variant
If xWs.Name <> "Prev Month" And xWs.Name <> "Current Month" Then

Set rng = Worksheet.AutoFilter.Range.Rows(1)

res = Application.Match("Diaper Range Premium (1) Mainline (2)", rng, 0)

rng.AutoFilter Field:=res, Criteria1:="MAINLINE"

Dim rng1 As Range, res1 As Variant
Set rng1 = Worksheet.AutoFilter.Range.Rows(1)

res1 = Application.Match("Diaper Type (1) - Taped (2) - Pants (9) - Unspecified", rng1, 0)

rng1.AutoFilter Field:=res1, Criteria1:="PANTS"

End If

End Sub