Solved

VBA Excel Code

Posted on 2011-02-23
8
289 Views
Last Modified: 2012-05-11
Hi...

We have many speadsheets within our workplace that alot of people have access to.

These spreadsheets have many sheets (tabs) which all have auto filters set.

The problems we have are people are leaving the auto filters on and then when somebody else goes in the spreadsheet they are reporting wrong information because they havent relaised the filter is still on.

I have in place a macro that removes all filters upon opening the spreadsheet put this currently only works on the first (active) sheet.

The code i have at the moment is below:

     'put the autofilter back on if it has been removed.
    If ActiveSheet.AutoFilterMode = False Then Rows(1).AutoFilter
     'error handle the showalldata part
    On Error Resume Next
    For Each ws In ActiveWorkbook.ShowAllData
    On Error GoTo 0

Is there something i can change for it to apply to all sheets.

Thank You.
0
Comment
Question by:SterlingGreen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 34959494
  Dim ws As Worksheet
   For Each ws In ThisWorkbook.Worksheets
      ws.Rows(1).AutoFilter
   Next ws

Kevin
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 250 total points
ID: 34959509
Actually, that should be:

   Dim ws As Worksheet
   For Each ws In ThisWorkbook.Worksheets
      ws.Rows(1).AutoFilter
      ws.Rows(1).AutoFilter
   Next ws

Kevin
0
 

Author Comment

by:SterlingGreen
ID: 34959642
when i use the code given it give me a Microsoft Visual Basic Error

Run-Time Error 1004

The command could not be completed by using the range specified. Select a single cell within the range and try the command again.

Behind the error it does work the filters have been reset.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 34959659
Do you have header values in row 1 of each worksheet?

Kevin
0
 

Author Comment

by:SterlingGreen
ID: 34959670
yes i'm testing this on a test spreadsheet at the moment so A1 and B1 have "name" and "code" in.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 34959686
Then you shouldn't get any errors. Are there ANY worksheets in the workbook that do not have headers and which you do not want to be filtered?

Kevin
0
 

Author Comment

by:SterlingGreen
ID: 34959701
Excellent it worked, because it was just a test spreadsheet i had sheet 1 sheet 2 and sheet 3.

sheet 3 wasnt populated....

Just deleted and No errors...

Thank you very much

Daniel
0
 

Author Closing Comment

by:SterlingGreen
ID: 34959704
very quick
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question