+ Reply to Thread
Results 1 to 5 of 5

Perform a macro on multiple worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    06-24-2008
    Posts
    23

    Perform a macro on multiple worksheets

    I tried to search this out on the site but couldn't seem to find an iteration that I could make enough sense out of for my use.

    I have the following code and what I'd like to do is have this same code performed on 28 other worksheets in the same workbook. I'm sure I could copy and repeat the same code 28 more times, but I've got to imagine there is some way to get a macro to tab through each sheet and perform the macro.

    The other sheets are named "Network 1" through "Network 28"

    
    Sub Autonumber()
    
    Dim LastRow As Long
    LastRow = Range("J65536").End(xlUp).Row
        
    
    Sheets("Master Disruption").Select
    Range("A2").Formula = "=if(J2>0,row(A1),"""")"
    Range("A2").AutoFill Destination:=Range("A2:A" & LastRow), Type:=xlFillDefault
    ActiveSheet.Range("A2", ActiveSheet.Range("A2").End(xlDown)).Copy
    ActiveSheet.Range("A2", ActiveSheet.Range("A2").End(xlDown)).PasteSpecial (xlPasteValues)
    
    
    End Sub
    Thanks in advance!

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Perform a macro on multiple worksheets

    Try this
    Option Explicit
    
    Sub Autonumber()
        Dim ws As Worksheet
        Dim LastRow As Long
    
        For Each ws In ThisWorkbook.Worksheets
            With ws
                LastRow = .Range("J65536").End(xlUp).Row
                .Range("A2").Formula = "=if(J2>0,row(A1),"""")"
                .Range("A2").AutoFill Destination:=.Range("A2:A" & LastRow), Type:=xlFillDefault
                .Range("A2", .Range("A2").End(xlDown)).Copy
                .Range("A2", .Range("A2").End(xlDown)).PasteSpecial (xlPasteValues)
            End With
        Next ws
    End Sub
    Last edited by royUK; 01-06-2011 at 03:43 PM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    06-24-2008
    Posts
    23

    Re: Perform a macro on multiple worksheets

    Thanks for the code. I forgot to mention one piece though. The Network 1 through Network 28 worksheets that I need to copy this to have to somehow be specifically mentioned. There are several other sheets in the workbook that I don't want this copied to.



    Quote Originally Posted by royUK View Post
    Try this
    Option Explicit
    
    Sub Autonumber()
        Dim ws As Worksheet
        Dim LastRow As Long
    
        For Each ws In ThisWorkbook.Worksheets
            With ws
                LastRow = .Range("J65536").End(xlUp).Row
                .Range("A2").Formula = "=if(J2>0,row(A1),"""")"
                .Range("A2").AutoFill Destination:=.Range("A2:A" & LastRow), Type:=xlFillDefault
                .Range("A2", .Range("A2").End(xlDown)).Copy
                .Range("A2", .Range("A2").End(xlDown)).PasteSpecial (xlPasteValues)
            End With
        Next ws
    End Sub

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Perform a macro on multiple worksheets

    Hello XL2008,

    This change will auto-number only the worksheets named "Network 1" through "Network 28".
    Sub Autonumber()
    
      Dim LastRow As Long
      Dim ws As Worksheet
     
        For Each ws In ThisWorkbook.Worksheets
          Select Case LCase(ws.Name)
            Case "network 1" To "network 28"
              With ws
                LastRow = .Range("J65536").End(xlUp).Row
                .Range("A2").Formula = "=if(J2>0,row(A1),"""")"
                .Range("A2").AutoFill Destination:=.Range("A2:A" & LastRow), Type:=xlFillDefault
                .Range("A2", .Range("A2").End(xlDown)).Copy
                .Range("A2", .Range("A2").End(xlDown)).PasteSpecial (xlPasteValues)
              End With
          End Select
        Next ws
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Perform a macro on multiple worksheets

    Or
    
    Sub Autonumber()
        Dim ws As Worksheet
        Dim LastRow As Long
    
        For Each ws In ThisWorkbook.Worksheets
            If Left(ws.Name, 7) = "Network" Then
                With ws
                    LastRow = .Range("J65536").End(xlUp).Row
                    .Range("A2").Formula = "=if(J2>0,row(A1),"""")"
                    .Range("A2").AutoFill Destination:=.Range("A2:A" & LastRow), Type:=xlFillDefault
                    .Range("A2", .Range("A2").End(xlDown)).Copy
                    .Range("A2", .Range("A2").End(xlDown)).PasteSpecial (xlPasteValues)
                End With
            End If
        Next ws
    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