Consulting

Results 1 to 9 of 9

Thread: Having trouble looping macro through active worksheets

  1. #1

    Having trouble looping macro through active worksheets

    This should be simple but I am having trouble. I am attempting to run a macro to hide columns through multiple worksheets of a workbook. but I do not want it to run through specific worksheet names. I tried the following but it is grouping this column 8 time on the first worksheet it finds past the excluded worksheets and then giving me a runtime error. Any idea on what I am getting wrong and how to fix?

    Sub Columns_Group()
        Dim ws As Worksheet
        On Error Resume Next
        For Each ws In ActiveWorkbook.Worksheets
        If Not ws.Name = "Individual Summary" 
        And Not ws.Name = "Leadership Summary" 
        And Not ws.Name = "Pivotsum" 
        And Not ws.Name = "LeadershipPivot" 
        And Not ws.Name = "Starspivot" A
        And Not ws.Name = "Stars Summary" A
        And Not ws.Name = "Full Data Sheet" Then Columns("C : D").Columns.Group
    Next ws
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I added [CODE] ... [/CODE] tags around your macro. You can do that using the [#] icon

    Where to start?

    1. Resume Next hides errors and probably shouldn't be used

    2. You have to use the line continuation character (_ or underscore )if you divide one logical line onto several physical lines

    If Not ws.Name = "Individual Summary" _
    3. I don't know what the "A" is for on the last two sheet name lines

    4. The .Columns is redundant.
     Columns("C:D").Group
    is sufficient

    5. Just
    Columns("C:D")
    without a worksheet will always use the ActiveSheet. Probably use
    ws.Columns("C:D").Group

    I'd simplify (in my opinion) and use Select Case. Not tested but this is the idea

    Option Explicit
    
    Sub Columns_Group()
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
            Select Case ws.Name
                Case "Individual Summary", "Leadership Summary", "Pivotsum", "LeadershipPivot", "Starspivot", "Stars Summary", "Full Data Sheet"
                         'do nothing
                Case Else
                    ws.Columns("C:D").Group
            End Select
        Next ws
    End Sub


    Or your original

     
    Sub Columns_Group1()
        Dim ws As Worksheet
    
        For Each ws In ActiveWorkbook.Worksheets
            If Not ws.Name = "Individual Summary" _
                And Not ws.Name = "Leadership Summary" _
                And Not ws.Name = "Pivotsum" _
                And Not ws.Name = "LeadershipPivot" _
                And Not ws.Name = "Starspivot" _
                And Not ws.Name = "Stars Summary" _
                And Not ws.Name = "Full Data Sheet" Then
                    ws.Columns("C:D").Group
            End If
        Next ws
    End Sub
    Last edited by Paul_Hossler; 05-18-2017 at 01:14 PM. Reason: undoing the emoticons
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Thanks for the response Paul. I've tried it both ways (with your modifications to my original and your suggested new way) both are giving me the same result. It's repeating the grouping 8 times on the same worksheet then giving me a runtime error.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    It's repeating the grouping 8 times on the same worksheet then giving me a runtime error.
    Was it the Active Sheet? Are there 8 sheets not Excluded? Is there a merged cells in Columns C or D on any sheet?

    Try viewing a different sheet before running the macro.

    I like InStr() to find the excluded sheets.
    Sub Columns_Group() 
        Dim ws As Worksheet 
    
    Const Excluded As String = "Individual Summary Leadership Summary Pivotsum LeadershipPivot Starspivot Stars Summary Full Data Sheet" 
     
       For Each ws In ActiveWorkbook.Worksheets 
            If InStr(Excluded,  ws.Name) = 0 then ws.Columns("C:D").Group 
        Next ws 
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by mccabe2017 View Post
    Thanks for the response Paul. I've tried it both ways (with your modifications to my original and your suggested new way) both are giving me the same result. It's repeating the grouping 8 times on the same worksheet then giving me a runtime error.
    Doesn't do that for me

    Are you sure you have the "ws." in this line?

    ws.Columns("C:D").Group
    is the macro in the active workbook?
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Post a blank copy of the workbook so we can test on your setup. We can't know of merged cells, spelling errors etc. which might affect the outcome. Paul's solution works for me.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Perhaps

    Dim oneSheet as Worksheet
    
    For Each oneSheet in ActiveWindow.SelectedSheets
        ' ....
    Next oneSheet

  8. #8
    I'm going to upload a file here. I even ran into the same sort of issue when using the Grouping file Paul was kind enough to provide. I found another code variant I decided to use as I thought that calling out the specific sheets would work better than trying to use if/then syntax or select case.

    But again, I continue to run into problem where the code seems to run continuously on the first worksheet in the array until I get an error message. I can not get any of the suggested methods to work through each worksheet.
    Attached Files Attached Files

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Simplifying to just the loop part ...

    The Array() has "Curly", but the workbook has a sheet named "Cury"


    Option Explicit
    Sub Test2()
      Dim InxW As Long
      Dim WshtNames As Variant
      WshtNames = Array("Moe", "Larry", "Curly", "Spongebob", "Squarepants")
      
      For InxW = LBound(WshtNames) To UBound(WshtNames)
        With Worksheets(WshtNames(InxW))
                
            MsgBox InxW & " -- " & .Name
        
        End With
      Next InxW
    End Sub


    Edit -- part 2

    You need the 'dot' when you're working with a 'With' otherwise it most likely will default to the ActiveSheet

    BTW, not need to select something to work on it (usually)

    Option Explicit
    Sub Test2()
      Dim InxW As Long
      Dim WshtNames As Variant
      WshtNames = Array("Moe", "Larry", "Curly", "Spongebob", "Squarepants")
      
      For InxW = LBound(WshtNames) To UBound(WshtNames)
        With Worksheets(WshtNames(InxW))
            .Columns("C:D").Group
            .Columns("G:H").Group
            .Columns("J:J").Group
            .Columns("L:N").Group
            .Columns("P:W").Group
            .Columns("Z:AC").Group
            .Columns("AF:AG").Group
            .Columns("AL:AL").Group
            .Columns("AN:AN").Group
            .Columns("AP:AP").Group
            .Columns("AR:AR").Group
            .Columns("AV:AV").Group
            .Columns("BA:BC").Group
            .Columns("BE:BE").Group
            .Columns("BH:BK").Group
            .Columns("BR:BT").Group
            .Columns("BW:BZ").Group
            .Columns("B:B").ColumnWidth = 19.29
            .Columns("G:H").ColumnWidth = 0
            .Columns("F:F").ColumnWidth = 22.57
            .Select
            .Range("AD2").Select
            ActiveWindow.FreezePanes = True
        End With
      Next InxW
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 06-07-2017 at 07:46 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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