+ Reply to Thread
Results 1 to 6 of 6

Use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master'

Hybrid View

  1. #1
    Registered User
    Join Date
    05-01-2023
    Location
    Singapore
    MS-Off Ver
    (Version 2302 Build 16.0.16130.20378) 64-bit
    Posts
    66

    Use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master'

    Hi experts!

    I am looking to use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master' to be placed in the second sheet position.

    The data worksheets all have the same table format with data from Column A to T, I wish to only keep the header row (row 1) ONCE and start the data from row 2 onwards.

  2. #2
    Forum Expert wk9128's Avatar
    Join Date
    08-15-2020
    Location
    Australia
    MS-Off Ver
    365 V2403 and WPS V2022
    Posts
    3,526

    Re: Use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master'

    Hi webcam723
    Please update your profile as necessary to properly reflect the exact version(s) of Excel your question relates to.
    Members tailor answers based on your Excel version. Your profile does not indicate your version 16.72.

    Pls try this code

    Sub ConsolidateData()
        Dim ws As Worksheet
        Dim masterWs As Worksheet
        Dim lastRow As Long
        Dim rngSource As Range
        Dim rngDestination As Range
        
        ' Create a new worksheet called "Master" and place it in the second sheet position
        Set masterWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(1))
        masterWs.Name = "Master"
        
        ' Loop through each worksheet starting from the second sheet
        For Each ws In ThisWorkbook.Sheets
            If ws.Index <> masterWs.Index Then
                ' Find the last row in the source worksheet
                lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
            
                ' Set the source range (excluding the header row)
                Set rngSource = ws.Range("A2:T" & lastRow)
            
                ' Find the last row in the destination sheet
                lastRow = masterWs.Cells(masterWs.Rows.Count, "A").End(xlUp).Row
            
                ' Set the destination range in the master worksheet
                Set rngDestination = masterWs.Range("A" & lastRow + 1)
            
                ' Copy the source range to the destination range
                rngSource.Copy rngDestination
            End If
        Next ws
    End Sub
    Last edited by wk9128; 07-12-2023 at 10:59 PM.

  3. #3
    Registered User
    Join Date
    05-01-2023
    Location
    Singapore
    MS-Off Ver
    (Version 2302 Build 16.0.16130.20378) 64-bit
    Posts
    66

    Re: Use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master'

    For Each ws In ThisWorkbook.Sheets(2).Worksheets

    In the above line of code, the error 'Object doesnt support this property or method' occurs.

  4. #4
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: Use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master'

    Hello Webcam,

    Create the Master sheet manually and place your required headings in row1, then try this simplified code:

    Sub Test()
    
        Dim ws As Worksheet, wsM As Worksheet
        Set wsM = Sheets("Master")
        
        Application.ScreenUpdating = False
        
        wsM.UsedRange.Offset(1).Clear
        
            For Each ws In Worksheets
                  If ws.Name <> "Master" Then
                       ws.UsedRange.Offset(1).Copy wsM.Range("A" & Rows.Count).End(3)(2)
                  End If
            Next ws
        
        Application.ScreenUpdating = True
    
    End Sub
    There is no need to have a new sheet created each time the code is run. The above code clears the Master sheet each time that the code is run.

    I hope that this helps.

    Cheerio,
    vcoolio.

  5. #5
    Registered User
    Join Date
    05-01-2023
    Location
    Singapore
    MS-Off Ver
    (Version 2302 Build 16.0.16130.20378) 64-bit
    Posts
    66

    Re: Use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master'

    Hi thanks alot!

    Ok so now my 'Master' sheet is the first sheet. I got another sheet in second position that I dont want to use the data from.

    How can i edit the code such that it uses the 3rd sheet onwards?

  6. #6
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: Use VBA to consolidate data from sheets 2 onwards into one new sheet called 'Master'

    Hello Webcam,

    Just exclude the other sheet as well:

    Sub Test()
    
        Dim ws As Worksheet, wsM As Worksheet
        Set wsM = Sheets("Master")
        
        Application.ScreenUpdating = False
        
        wsM.UsedRange.Offset(1).Clear
        
            For Each ws In Worksheets
                  If ws.Name <> "Master" And ws.Name <> "Sheet2" Then  '---->Change "Sheet2" to suit.
                       ws.UsedRange.Offset(1).Copy wsM.Range("A" & Rows.Count).End(3)(2)
                  End If
            Next ws
        
        Application.ScreenUpdating = True
    
    End Sub
    I hope that this helps.

    Cheerio,
    vcoolio.

+ 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. How to consolidate data from multiple sheets onto single master sheet
    By Evanexcel in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-22-2022, 10:16 AM
  2. Consolidate several sheets data in a master sheets
    By amy03 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-24-2016, 06:42 PM
  3. [SOLVED] Macro to Create a Master Sheet and consolidate data as per Headings on all Sheets
    By prkhan56 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-21-2015, 01:32 PM
  4. [SOLVED] How to consolidate all data from multiple sheets into a master table.
    By marcusduton in forum Excel General
    Replies: 0
    Last Post: 02-26-2015, 11:55 PM
  5. Replies: 1
    Last Post: 07-17-2014, 12:01 PM
  6. Consolidate all the sheets into one master sheet
    By vignesh rocks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2013, 08:41 AM
  7. Replies: 0
    Last Post: 07-20-2012, 04:44 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