+ 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
    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

  2. #2
    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!

  3. #3
    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

  4. #4
    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!

  5. #5
    Registered User
    Join Date
    04-05-2007
    Location
    Sydney Australia
    Posts
    48
    Using that piece of code the action is performed only on the sheet called DataSheet and not on any of the others.

    I think i understand what you are trying to do but it seems to be working in reverse??????

    Jeff

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

    This time for sure

    Ok give this one a go!

    Sub test()
    Dim wsSheet As Worksheet
    
        For Each wsSheet In Worksheets
        Select Case wsSheet.Name
        Case "ListSheet"
        
        Case "DataSheet"
    
        Case Else
        wssheet.select
        'Your Code Here
        End Select
        Next wsSheet
    End Sub
    Last edited by Steel Monkey; 05-03-2007 at 12:34 AM.

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

    Silly Silly Steel Monkey

    You can use either of the two examples above but if you use the first one just add wssheet.select and it will work perfectly:


    Sub test()
    Dim wsSheet As Worksheet
    
        For Each wsSheet In Worksheets
            If wsSheet.Name <> "DataSheet" Then
                  If wsSheet.Name <> "ListSheet" Then
                  wsSheet.Select ' we need to add this line
                  'yourcodehere
                  End If
        End If
        
        Next wsSheet
    End Sub

+ 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