+ Reply to Thread
Results 1 to 11 of 11

Exclude some sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    04-05-2007
    Location
    Sydney Australia
    Posts
    48

    Exclude some sheets

    Hi All

    I would like to run a macro on all the sheets in a workbook except for two sheets named DataSheet and ListSheet. The remaining sheets are named Sheet1 Sheet2 etc. The number of sheets may vary from 2 through to 20.

    I have searched these forums and found reference to this subject including various examples of code which handles this task.

    Unfortunately i can't seem to adapt any of them to do exactly what i want

    Any assistance would again be very appreciated.

    Thanks

    jeff

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,446
    Sub loopsheets()
    Dim sheetnumber As Variant
    For sheetnumber = 1 To 20
    Worksheets("Sheet" & sheetnumber).Select
    Your code here
    Next
    End Sub
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    04-05-2007
    Location
    Sydney Australia
    Posts
    48
    Hi Dave

    Thanks for your reply.

    Your code works great if there are exactly 20 sheets in the workbook. The number may vary however from 2 up to 20 so i need it to run without specifying exact numbers of sheets. Can you help further??

    Thanks again

    Jeff

  4. #4
    Forum Contributor
    Join Date
    11-23-2005
    Location
    Perth, Australia
    Posts
    218

    Exclude some sheets

    Add something like the following:
    Dim wscount As Integer
    
    wscount = Application.Worksheets.Count
    Then change:

    For sheetnumber = 1 To 20
    to

    For sheetnumber = 1 To wscount
    That should do what you need

    Let me know how it goes!

  5. #5
    Registered User
    Join Date
    04-05-2007
    Location
    Sydney Australia
    Posts
    48
    thanks Steel Monkey

    Your suggestions did do as i wanted however the code runs correctly and then i get a subscript out of range error on the highlighed line

    Dim wscount As Integer
    
    wscount = Application.Worksheets.Count
    Dim sheetnumber As Variant
    For sheetnumber = 1 To wscount
    Worksheets("Sheet" & sheetnumber).Select
    
    Range("a1").ClearContents
    
    Next
    End Sub
    What do you think?

    Thanks

    Jeff

  6. #6
    Forum Contributor
    Join Date
    11-23-2005
    Location
    Perth, Australia
    Posts
    218
    Thta makes sense as it would be trying to select a sheet called "sheetDataList" which isnt there.

    What you would be better off doing is looping through ALL sheets and not setting a number to loop through. Please see the below code which will loop through all sheets and check if the currentsheet is named Datasheet or Listsheet and if if it will go to the next sheet.

    Enter your code on what you want done and it should work fine

    Sub test()
    Dim wsSheet As Worksheet
    
        For Each wsSheet In Worksheets
            If wsSheet.Name <> "DataSheet" Then
                  If wsSheet.Name <> "ListSheet" Then
                  'your code here
                  End If
        End If
        
        Next wsSheet
    End Sub
    Let me know how you go!

+ 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