+ Reply to Thread
Results 1 to 27 of 27

Code for multiple worksheets

  1. #1
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479

    Code for multiple worksheets

    I am using excel 2000

    How can I make the following code (saved in the This Workbook sheet)available to all or multiple sheets in my workbook

    Private Sub Workbook_Open()
    'check for filter, turn on if none exists
    With Worksheets("Missing Data")
    If Not .AutoFilterMode Then
    .Range("A3").AutoFilter
    End If
    .EnableAutoFilter = True
    .Protect _
    Contents:=True, UserInterfaceOnly:=True
    End With
    End Sub
    Last edited by Paul Sheppard; 06-13-2008 at 12:20 PM.
    Paul

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    That's in the WorkBook module, which bit do you want to make aplicable to all sheets?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    That's in the WorkBook module, which bit do you want to make aplicable to all sheets?
    This code only works on the worksheet called Missing Data, how can I make it work on other worksheets in the same workbook

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try this

    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    Try this

    Please Login or Register  to view this content.
    I have tried this and in the above code .AutoFilterMode is highlighted, with the message Compile Error: Invalid or unqualified reference

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    try this

    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    try this

    Please Login or Register  to view this content.
    I have tried this and in the above code .EnableAutoFilter = is highlighted, with the message Compile Error: Invalid or unqualified reference

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try this

    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    Try this

    Please Login or Register  to view this content.
    This just seems to loop through all the sheets in the workbook and make sure they are protected, but does not allow me to filter on any sheets

  10. #10
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Hi Paul,

    Is there any data in your sheets?

    When I tried roy's code on an EMPTY workbook it did as you said and just looped and protected the sheets but when I put some data (but only in Cells A2, A3, A4 or B4) on any sheet it turned on the autofilter. If data were put in ANY OTHER cell it did not turn on the autofilter.
    Hope this helps

    Seamus

  11. #11
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by SOS
    Hi Paul,

    Is there any data in your sheets?

    When I tried roy's code on an EMPTY workbook it did as you said and just looped and protected the sheets but when I put some data (but only in Cells A2, A3, A4 or B4) on any sheet it turned on the autofilter. If data were put in ANY OTHER cell it did not turn on the autofilter.
    Hi SOS

    I have data down to row 82

    Row 1 is a Header Row
    Row 2 is Blank
    Row 3 contains Column Headings
    Rows 4-81 contain my data

    and

    Row 82 Totals

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The code sets the filter to Row 3, you need an empty row between the header Row & Totals. Add a row & change A3 to A4, see if that works

  13. #13
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    The code sets the filter to Row 3, you need an empty row between the header Row & Totals. Add a row & change A3 to A4, see if that works
    Hi ROYUK

    Have added a blank row and changed A3 to A4, if I save file with filters off and sheets unprotected the next time I open the file the filters appear and work, if I then save the file with the filters on , the next time I open the file the filters do not work

    I need the sheets to be protected all the time as the file is sent to various people and they should not have access to make changes
    Last edited by Paul Sheppard; 06-16-2008 at 03:52 AM.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    It doesn't do that for me
    Attached Files Attached Files

  15. #15
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Hi ROYUK

    Thanks for the file, but it doesn't work for me

    Would it be easier if we went back to my original code, as it worked for one worksheet called Missing Data

    Please Login or Register  to view this content.
    and just added specific worksheets, the one I need is called Commission

  16. #16
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Do you want to filter all sheets or just the one?

  17. #17
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    2 sheets "Missing Data" and "Commission"

  18. #18
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You could use the Workbook_SheetActivate event. This will check for a Filter whenever you activate either sheet.

    Please Login or Register  to view this content.
    Last edited by royUK; 11-03-2008 at 12:05 PM.

  19. #19
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Hi ROYUK

    Have tried that but still cannot get ti to work.

    I have saved your file and opened it and that does not work either

  20. #20
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    It works for me, are you enabling macros?

  21. #21
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    It works for me, are you enabling macros?
    Yes

    Could it be to do with add ins? I only have Analysis Toolpak, Analysis Toolpak - VBA and Solver Add-In selected?

  22. #22
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I have added a couple of mesageboxes that say what the macro is doing.

    Addins should not affect this code.
    Last edited by royUK; 11-03-2008 at 12:05 PM.

  23. #23
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    I have added a couple of mesageboxes that say what the macro is doing.

    Addins should not affect this code.
    Saved the file, it opened on the Commission worksheet, the filter did not work, so clicked on the Missing Data worksheet and got a message box with the message "Filter on"

  24. #24
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The code works when you activate the sheet, so go back to the other sheet & it should work. If you want it to work on opening then move the code to WorkBook_Open

  25. #25
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    The code works when you activate the sheet, so go back to the other sheet & it should work. If you want it to work on opening then move the code to WorkBook_Open
    I opened on the file it opened with Commission worksheet, the filter did not work, so clicked on the Missing Data worksheet and got a message box with the message "Filter on", clicked OK filter still did not work, so clicked on the "Missing Data" worksheet, the message box with the message "Filter on" appeared, clicked OK filter still did not work

    I would like it to work on opening the workbook, but do not know what you mean by
    move the code to WorkBook_Open
    Sorry not v good at VB

  26. #26
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    the macro runs when the workbook is opened
    Attached Files Attached Files

  27. #27
    Valued Forum Contributor
    Join Date
    06-30-2005
    Location
    Verwood, Dorset, England
    MS-Off Ver
    Excel 2000
    Posts
    479
    Quote Originally Posted by royUK
    the macro runs when the workbook is opened
    Hi RoyUK

    Thanks for all your help and patience with this. The last file you posted still did not work. Have now managed to solve the problem with the following code

    Please Login or Register  to view this content.
    Once again much appreciate your help and patience

    Paul

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1