+ Reply to Thread
Results 1 to 5 of 5

Need to repeat macro across multiple worksheets in workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2014
    Posts
    3

    Need to repeat macro across multiple worksheets in workbook

    Hello,

    I have no experience with VBA so need help in figuring this out. I have the following code:

    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer

    Sub Overtime()
    '
    ' Overtime Macro
    ' Calculate Overtime
    '
    
    '
        Range("A1:R1").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Columns("O:O").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("O3").Select
        ActiveCell.FormulaR1C1 = "Overtime"
        With ActiveCell.Characters(Start:=1, Length:=8).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
        Range("O4").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(RC13<0.00001,"""",IF(RC13>0.6458333,RC13-0.6458333,""""))"
        Range("O4").Select
        Selection.AutoFill Destination:=Range("O4:O19"), Type:=xlFillDefault
        Range("O4:O19").Select
        Range("O20").Select
        ActiveCell.FormulaR1C1 = "=SUM(R[-16]C:R[-1]C)"
        Range("O21").Select
        ActiveWindow.SmallScroll ToRight:=4
        Range("A1:S1").Select
        Range("S1").Activate
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        ActiveWindow.LargeScroll ToRight:=-1
        Range("A20").Select
    End Sub

    I want to be able to have it repeat the sheet formatting and calculation across multiple worksheets...how do i do that?
    Last edited by 6StringJazzer; 06-13-2014 at 12:42 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Need to repeat macro across multiple worksheets in workbook

    If you want for all sheets in that workbook. add the below line just before Range("A1:R1").Select
    Dim Sht As Worksheet
    For Each Sht In ActiveWorkbook.Sheets
    And this line just before End Sub
    Next Sht

  3. #3
    Registered User
    Join Date
    05-22-2014
    Posts
    3

    Re: Need to repeat macro across multiple worksheets in workbook

    Thanks but I need the macro to run on each sheet...also I only want it to run on certain sheets, in this case "Mon", "Tue", "Wed", "Thurs", "Fri" - I want to exclude "Sat", "Sun" & "Totals" worksheets.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,772

    Re: Need to repeat macro across multiple worksheets in workbook

    This code will loop through all worksheets in the file. I have also cleaned up the code for macro recorder bloat. I believe this can be cleaned up quite a bit more, as your formatting include settings that are defaults (e.g., .WrapText = False). However, I don't know what settings are in your sheets in the first place and what you really need to change.

    I have compiled but not tested this. It appears that to do a decent test I would have to have your workbook with its data and formatting.

    Sub Overtime()
    '
    ' Overtime Macro
    ' Calculate Overtime
    '
    
    '
       Dim ws As Worksheet
       
       For Each ws In Worksheets
       
          With ws
          
             With .Range("A1:R1")
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
             End With
             
             .Columns("O:O").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
             
             With .Range("O3")
                .FormulaR1C1 = "Overtime"
                With .Characters(Start:=1, Length:=8).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
             End With
             
             .Range("O4").FormulaR1C1 = _
                "=IF(RC13<0.00001,"""",IF(RC13>0.6458333,RC13-0.6458333,""""))"
             .Range("O4").AutoFill Destination:=Range("O4:O19"), Type:=xlFillDefault
             .Range("O20").FormulaR1C1 = "=SUM(R[-16]C:R[-1]C)"
             .Range("O21").Select
             ActiveWindow.SmallScroll ToRight:=4
             With .Range("S1")
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = True
             End With
          
          End With
       Next ws
       
       ActiveWindow.LargeScroll ToRight:=-1
       Range("A20").Select
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,772

    Re: Need to repeat macro across multiple worksheets in workbook

    Replace the For Each statement with the following code:

       Dim SheetNames() As String
       SheetNames = Array("Mon", "Tue", "Wed", "Thurs", "Fri")
       Dim WSName As String
       For Each WSName In SheetNames
           Set WS = Worksheets(WSName)
    and replace

    Next ws
    
    'with
    
    Next WSName

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to send multiple worksheets, but not whole workbook, to one individual
    By dms2228 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2013, 09:48 AM
  2. Macro to pdf multiple worksheets from one workbook
    By ljuarez714 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-27-2013, 04:28 PM
  3. [SOLVED] macro for saving multiple worksheets in to a new workbook
    By ste1605 in forum Excel General
    Replies: 7
    Last Post: 05-08-2012, 10:05 AM
  4. VBA to Repeat over all worksheets in workbook
    By auscaf in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-03-2010, 09:02 PM
  5. [SOLVED] Repeat printing rows in multiple worksheets
    By KevinG in forum Excel General
    Replies: 13
    Last Post: 05-23-2006, 04:10 PM

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