+ Reply to Thread
Results 1 to 6 of 6

Need to modify VBA Code that copies multiple sheets to a master sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2014
    Location
    NC
    MS-Off Ver
    Excel 2010
    Posts
    3

    Need to modify VBA Code that copies multiple sheets to a master sheet

    I found the code below and it works perfectly if I want to copy all the other sheets to a master sheet. But, I need to specify specific sheets. Basically I have a workbook consisting of multiple sheets and multiple "master" sheets so I need to specify in the code which sheets it should be copying.

    Private Sub Worksheet_Activate()
    Dim Sheet As Worksheet
    
    For Each Sheet In Me.Parent.Sheets
        If Sheet.Name <> Me.Name Then
            If Sheet.Cells(Rows.Count, 1).End(xlUp).Row <> 1 Then
                Sheet.Range(Sheet.Cells(2, 1), Sheet.Cells(Sheet.Cells(Rows.Count, 1).End(xlUp).Row, 18)).Copy Destination:=Me.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    
            End If
        Else
            Me.Range(Cells(2, 1), Cells(Rows.Count, 18)).Clear
        End If
    
    Next Sheet
    End Sub
    I have found this question asked before but never ran across any useful answers. I am an extreme rookie when it comes to VBA.
    Any advise would be greatly appreciated.

  2. #2
    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: Need to modify VBA Code that copies multiple sheets to a master sheet

    Hello AXGirl,

    You can approach this problem in two ways. Either examine a list of sheet names you want to use or examine a list of sheet names to exclude. The choice really depends on which list is shorter. Generally, the exclusion list is shorter.

    Private Sub Worksheet_Activate()
    
        Dim Sheet As Worksheet
    
        For Each Sheet In Me.Parent.Sheets
            Select Case Sheet.Name
                Case Is = "Master", Me.Name
                    ' Do Nothing - These are the sheets you want to skip
                Case Else
                    If Sheet.Cells(Rows.Count, 1).End(xlUp).Row <> 1 Then
                        Sheet.Range(Sheet.Cells(2, 1), Sheet.Cells(Sheet.Cells(Rows.Count, 1).End(xlUp).Row, 18)).Copy Destination:=Me.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                    Else
                        Me.Range(Cells(2, 1), Cells(Rows.Count, 18)).Clear
                    End If
            End Select
        Next Sheet
        
    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!)

  3. #3
    Registered User
    Join Date
    03-20-2014
    Location
    NC
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Need to modify VBA Code that copies multiple sheets to a master sheet

    Thank you so much for your reply. I will definetly make note of this. It could come in handy in other situations. But, in my current case I will end up with 6 "Master" sheets that will each have any where from 5 to 10 sheets that will need to be copied to it. Listing the actual sheets would be a better option. Could I ask you to provide a code sample of the follow scenario?
    Master sheet is named "Master"
    Need to copy from sheets named "A", "C", & "E"

    I'm not that good with VBA but trying to learn. Its about my only weak spot when it comes to Excel.

    Thanks a Bunch,
    AXGirl

  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: Need to modify VBA Code that copies multiple sheets to a master sheet

    Hello AXGirl,

    Switch the statements around like this...
    Private Sub Worksheet_Activate()
    
        Dim Sheet As Worksheet
    
        For Each Sheet In Me.Parent.Sheets
            Select Case Sheet.Name
                Case Is = "A", "C", "E"
                    If Sheet.Cells(Rows.Count, 1).End(xlUp).Row <> 1 Then
                        Sheet.Range(Sheet.Cells(2, 1), Sheet.Cells(Sheet.Cells(Rows.Count, 1).End(xlUp).Row, 18)).Copy Destination:=Me.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                    Else
                        Me.Range(Cells(2, 1), Cells(Rows.Count, 18)).Clear
                    End If
                Case Else
                    ' Do Nothing - These are the sheets you want to skip
            End Select
        Next Sheet
        
    End Sub

  5. #5
    Registered User
    Join Date
    03-20-2014
    Location
    NC
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Need to modify VBA Code that copies multiple sheets to a master sheet

    Perfect!! Works like a charm. Thank you so much for your assistance.

  6. #6
    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: Need to modify VBA Code that copies multiple sheets to a master sheet

    Hello AXGirl,

    You're welcome. Glad to help a fellow Tarheel.

+ 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. Replies: 14
    Last Post: 05-16-2013, 08:47 AM
  2. [SOLVED] Can i modify this code to apply to multiple checkboxes and sheets?
    By stolen_83 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 02:44 PM
  3. Replies: 2
    Last Post: 05-01-2013, 12:26 PM
  4. Copy sheet to another WB and macro copies that data to last row of master sheet
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2013, 06:31 PM
  5. Replies: 1
    Last Post: 02-08-2013, 10:36 PM

Tags for this Thread

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