Results 1 to 3 of 3

Multiiple Worksheet Loop Macro only Looping on 1 sheet

Threaded View

  1. #1
    Registered User
    Join Date
    09-18-2012
    Location
    Illinois
    MS-Off Ver
    Excel 2007
    Posts
    4

    Multiiple Worksheet Loop Macro only Looping on 1 sheet

    Good Morning,

    I have a Macro which calls 2 submacros to create and names multiple worksheets. This portion is working. Additionally, I have 2 sub Macros that I want to process through all the worksheets in the file (one is for sorting and subtotaling and one for print formatting). Unfortunately, the Looping macro I am using is looping the same
    worksheet data over and over instead of one time for each worksheet. Since the number of spreadsheets will be variable from file to file, I can't just define the spreadsheets once. Can someone help me with the correct code so that the Sub macros for Sorting&Subtotaling and Print formatting will work on any number of worksheets?

    Thank you in advance!
    LC

    ---------------------------------------------------------------

    Sub CostCenterMacro()
    '
    'Combines Sort and Subtotal Macro with print macro
    'Created by LC on 8/23/2012
    
    '
    Call SupplyChainSortsMultipleCostCenters                 this works already
    Call CostCenterstoSheets                                      this works already
    Call WorksheetLoop                                              this is the SubMacro that has 2 additional submacros embedded in it only wrking on last spreadsheet    
    
    End Sub
    ---------------------------------------------------------------
    Sub WorksheetLoop()
    
       Dim WS_Count As Integer
       Dim I As Integer
    
       ' Set WS_Count equal to the number of worksheets in the active
       ' workbook.
       WS_Count = ActiveWorkbook.Worksheets.Count
    
       ' Begin the loop.
       For I = 1 To WS_Count
    
          ' Insert your code here.
          
          Call SupplyChainSortSubTtl
          Call PrintSetup2
          ' The following line shows how to reference a sheet within
          ' the loop by displaying the worksheet name in a dialog box.
          MsgBox ActiveWorkbook.Worksheets(I).Name
    
       Next I
    
    End Sub
    ----------------------------------------------------------------------------
    Sub SupplyChainSortSubTtl()
    '
    ' SupplyChainSortSubTtl Macro
    '
    
    '
           
        Cells.Select
        lastrow = Range("a100000").End(xlUp).Row
        
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
        ActiveWorkbook.ActiveSheet.Sort.SortFields. _
            Add Key:=Range("D2:D" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.ActiveSheet.Sort.SortFields. _
            Add Key:=Range("G2:G" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.ActiveSheet.Sort.SortFields. _
            Add Key:=Range("N2:N" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.ActiveSheet.Sort
            .SetRange Range("A1:Q" & lastrow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Selection.Subtotal GroupBy:=4, Function:=xlSum, TotalList:=Array(12), _
            Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    End Sub
    -------------------------------------------------------------------------------------
    Sub PrintSetup2()
    '
    ' PrintSetup Macro
    ' Print Supply Chain Sort & Subtotal Created by LC on 8/24/2012
    '
    
    '
        Dim S As Integer
        Dim X As Integer
        
        X = Sheets.Count
        
        
        newname = Range("C2").Value
        Cells.Select
        ActiveSheet.Name = newname
        
        Cells.Select
        lastrow = Range("a100000").End(xlUp).Row
        With Selection.Font
            .Name = "Calibri"
            .FontStyle = "Regular"
            .Size = 11
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
        
        ActiveCell.Columns("A:J").EntireColumn.Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        ActiveCell.Offset(0, 11).Columns("m:q").EntireColumn.Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        ActiveCell.Offset(0, -1).Columns("L:L").EntireColumn.Select
        Selection.Style = "Comma"
        
            
        Rows("1:1").Select
        Range("B1").Activate
        With Selection
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Rows("1:1").EntireRow.AutoFit
        Columns("B:B").EntireColumn.AutoFit
        Columns("E:E").EntireColumn.AutoFit
        Columns("G:G").EntireColumn.AutoFit
        Columns("H:H").ColumnWidth = 3.5
        Columns("I:I").ColumnWidth = 5.25
        Columns("J:J").ColumnWidth = 5.25
        Columns("K:K").EntireColumn.AutoFit
        ActiveWindow.ScrollColumn = 3
        Columns("L:L").EntireColumn.AutoFit
        Columns("M:M").EntireColumn.AutoFit
        
        Range("A1:O1").Select
        Range("O1").Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent1
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
        Columns("P:Q").Select
        Selection.EntireColumn.Hidden = True
        Columns("A:A").Select
        Selection.EntireColumn.Hidden = True
        With ActiveSheet.PageSetup
            .PrintTitleRows = "$1:$1"
            .PrintTitleColumns = ""
        End With
        ActiveSheet.PageSetup.PrintArea = "$B:$O"
        With ActiveSheet.PageSetup
            .LeftHeader = ""
            .CenterHeader = "&A"
            .RightHeader = ""
            .LeftFooter = "Page &P of &N"
            .CenterFooter = ""
            .RightFooter = "&Z&F/&A" & Chr(10) & "&D &T"
            .LeftMargin = Application.InchesToPoints(0.2)
            .RightMargin = Application.InchesToPoints(0.2)
            .TopMargin = Application.InchesToPoints(0.75)
            .BottomMargin = Application.InchesToPoints(0.75)
            .HeaderMargin = Application.InchesToPoints(0.3)
            .FooterMargin = Application.InchesToPoints(0.3)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintComments = xlPrintNoComments
            .PrintQuality = 600
            .CenterHorizontally = False
            .CenterVertically = False
            .Orientation = xlLandscape
            .Draft = False
            .PaperSize = xlPaperLetter
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .BlackAndWhite = False
            .Zoom = 65
            .PrintErrors = xlPrintErrorsDisplayed
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = ""
            .EvenPage.RightHeader.Text = ""
            .EvenPage.LeftFooter.Text = ""
            .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = ""
            .FirstPage.CenterHeader.Text = ""
            .FirstPage.RightHeader.Text = ""
            .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
         
    End Sub

    Moderator Edit:

    Welcome to the forum, lscarstens.

    Please notice that code tags have been added to your post. The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.

    This is the second time in 2 days that you've been reminded.
    Thanks.
    Last edited by Cutter; 09-20-2012 at 02:29 PM. Reason: Added code tags

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