+ Reply to Thread
Results 1 to 4 of 4

Run Macros on all except "Summary" and "Reports" worksheets in a workbook

  1. #1
    Registered User
    Join Date
    07-09-2013
    Location
    Ketchikan, Alaska
    MS-Off Ver
    Excel 2003
    Posts
    2

    Run Macros on all except "Summary" and "Reports" worksheets in a workbook

    I have a workbook with a macro summarizing the data on the other sheets, but I need to add a "Reports" tab. What can I add/change in the code to exclude this tab?

    Here is the code that I have to include all of the worksheets:

    Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
    After:=sh.Range("A1"), _
    Lookat:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Row
    On Error GoTo 0
    End Function

    Function LastCol(sh As Worksheet)
    On Error Resume Next
    LastCol = sh.Cells.Find(What:="*", _
    After:=sh.Range("A1"), _
    Lookat:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Column
    On Error GoTo 0
    End Function

    Sub CopyRangeFromMultiWorksheets()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim CopyRng As Range

    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With

    ' Delete the summary sheet if it exists.
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("Summary").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    ' Add a new summary worksheet.
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "Summary"

    ' Loop through all worksheets and copy the data to the
    ' summary worksheet.
    For Each sh In ActiveWorkbook.Worksheets
    If sh.Name <> DestSh.Name Then

    ' Find the last row with data on the summary worksheet.
    Last = LastRow(DestSh)

    ' Specify the range to place the data.
    Set CopyRng = sh.Range("A2:K37")

    ' Test to see whether there are enough rows in the summary
    ' worksheet to copy all the data.
    If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
    MsgBox "There are not enough rows in the " & _
    "summary worksheet to place the data."
    GoTo ExitTheSub
    End If

    ' This statement copies values and formats from each
    ' worksheet.
    CopyRng.Copy
    With DestSh.Cells(Last + 1, "A")
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    End With

    ' Optional: This statement will copy the sheet
    ' name in the H column.
    DestSh.Cells(Last + 1, "L").Resize(CopyRng.Rows.Count).Value = sh.Name

    End If
    Next

    ExitTheSub:

    Application.Goto DestSh.Cells(1)

    ' AutoFit the column width in the summary sheet.
    DestSh.Columns.AutoFit

    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Macros on all except "Summary" and "Reports" worksheets in a workbook

    Dar,
    Welcome to the forum!
    Please use code tags with your code. Please read forum's rule.
    This code is Ron Deburin's code. It summarise the consolidate sheets in to a sheet called "Summary" Which bit do you want to change or add?

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Macros on all except "Summary" and "Reports" worksheets in a workbook

    Have not only change the code, but also I used code tags.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-09-2013
    Location
    Ketchikan, Alaska
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Run Macros on all except "Summary" and "Reports" worksheets in a workbook

    I haven't read the forum's rule yet. I'll make sure to go over it today.
    I just wanted to add a sheet labeled "Reports" but have it excluded from the existing Macro. What you had is perfect! Thank you!
    Last edited by daralea; 07-10-2013 at 05:55 PM.

+ 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