Copy multiple tabs and past as link into single tab

snai7247

New Member
Joined
Mar 20, 2023
Messages
3
Office Version
  1. 365
Hi,

Thanks in advance since i am pretty new to Excel VBA.

I have a excel file that contains multiple tabs with the same header but The # of rows can vary).

What i want to do is to merge all the data from each tab together into one tab. When copy and paste, paste as link so if a user changes the data in first tab it will reflect in "stacked master tab". For example, copy all data in first tab (include header) into a new tab, then copy the second,third...etc(without header) all as link.
 
Maybe something like this...

VBA Code:
Option Explicit

Const MASTER_SHEET_NAME As String = "Stacked Master"

Sub CombineWorksheets()

    Application.ScreenUpdating = False

    'delete worksheet called "Stacked Master", if one already exists
    On Error Resume Next
    Application.DisplayAlerts = False
    ThisWorkbook.Worksheets(MASTER_SHEET_NAME).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0

    'add new worksheet for the combined data
    Dim destinationWorksheet As Worksheet
    Set destinationWorksheet = ThisWorkbook.Worksheets.Add(before:=ThisWorkbook.Worksheets(1))
    destinationWorksheet.Name = MASTER_SHEET_NAME
    
    'combine the data from worksheets
    Dim copyRange As Range
    Dim nextRow As Long
    Dim sheetIndex As Long
    nextRow = 1
    For sheetIndex = 2 To ThisWorkbook.Worksheets.Count
        If sheetIndex > 2 Then
            With ThisWorkbook.Worksheets(sheetIndex).UsedRange
                Set copyRange = .Offset(1, 0).Resize(.Rows.Count - 1)
            End With
        Else
            Set copyRange = ThisWorkbook.Worksheets(sheetIndex).UsedRange
        End If
        copyRange.Copy
        With destinationWorksheet
            .Activate
            .Cells(nextRow, "a").Select
            .Paste Link:=True
        End With
        nextRow = nextRow + copyRange.Rows.Count
    Next sheetIndex
    
    destinationWorksheet.Range("a1").Select
    
    Application.CutCopyMode = False
    
    Application.ScreenUpdating = True
    
    MsgBox "The data has been combined in the 'Stacked Master' worksheet.", vbInformation
    
End Sub

Hope this helps!
 
Upvote 1
Maybe something like this...

VBA Code:
Option Explicit

Const MASTER_SHEET_NAME As String = "Stacked Master"

Sub CombineWorksheets()

    Application.ScreenUpdating = False

    'delete worksheet called "Stacked Master", if one already exists
    On Error Resume Next
    Application.DisplayAlerts = False
    ThisWorkbook.Worksheets(MASTER_SHEET_NAME).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0

    'add new worksheet for the combined data
    Dim destinationWorksheet As Worksheet
    Set destinationWorksheet = ThisWorkbook.Worksheets.Add(before:=ThisWorkbook.Worksheets(1))
    destinationWorksheet.Name = MASTER_SHEET_NAME
   
    'combine the data from worksheets
    Dim copyRange As Range
    Dim nextRow As Long
    Dim sheetIndex As Long
    nextRow = 1
    For sheetIndex = 2 To ThisWorkbook.Worksheets.Count
        If sheetIndex > 2 Then
            With ThisWorkbook.Worksheets(sheetIndex).UsedRange
                Set copyRange = .Offset(1, 0).Resize(.Rows.Count - 1)
            End With
        Else
            Set copyRange = ThisWorkbook.Worksheets(sheetIndex).UsedRange
        End If
        copyRange.Copy
        With destinationWorksheet
            .Activate
            .Cells(nextRow, "a").Select
            .Paste Link:=True
        End With
        nextRow = nextRow + copyRange.Rows.Count
    Next sheetIndex
   
    destinationWorksheet.Range("a1").Select
   
    Application.CutCopyMode = False
   
    Application.ScreenUpdating = True
   
    MsgBox "The data has been combined in the 'Stacked Master' worksheet.", vbInformation
   
End Sub

Hope this helps!
Thanks that was really helpful
 
Upvote 0
Maybe something like this...

VBA Code:
Option Explicit

Const MASTER_SHEET_NAME As String = "Stacked Master"

Sub CombineWorksheets()

    Application.ScreenUpdating = False

    'delete worksheet called "Stacked Master", if one already exists
    On Error Resume Next
    Application.DisplayAlerts = False
    ThisWorkbook.Worksheets(MASTER_SHEET_NAME).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0

    'add new worksheet for the combined data
    Dim destinationWorksheet As Worksheet
    Set destinationWorksheet = ThisWorkbook.Worksheets.Add(before:=ThisWorkbook.Worksheets(1))
    destinationWorksheet.Name = MASTER_SHEET_NAME
   
    'combine the data from worksheets
    Dim copyRange As Range
    Dim nextRow As Long
    Dim sheetIndex As Long
    nextRow = 1
    For sheetIndex = 2 To ThisWorkbook.Worksheets.Count
        If sheetIndex > 2 Then
            With ThisWorkbook.Worksheets(sheetIndex).UsedRange
                Set copyRange = .Offset(1, 0).Resize(.Rows.Count - 1)
            End With
        Else
            Set copyRange = ThisWorkbook.Worksheets(sheetIndex).UsedRange
        End If
        copyRange.Copy
        With destinationWorksheet
            .Activate
            .Cells(nextRow, "a").Select
            .Paste Link:=True
        End With
        nextRow = nextRow + copyRange.Rows.Count
    Next sheetIndex
   
    destinationWorksheet.Range("a1").Select
   
    Application.CutCopyMode = False
   
    Application.ScreenUpdating = True
   
    MsgBox "The data has been combined in the 'Stacked Master' worksheet.", vbInformation
   
End Sub

Hope this helps!
Hi, I also am looking for something like this. I have tried this and it partially worked however it only looked at the first 4 of my tabs. Do you know how i can get it to look at 500+?

Thanks
:)
 
Upvote 0
Hi,

Actually, as it stands, the code should automatically loop through every worksheet in the workbook running the code, and copy the data from each one of those worksheets to the master worksheet.

Cheers!
 
Upvote 0
Hi,

Actually, as it stands, the code should automatically loop through every worksheet in the workbook running the code, and copy the data from each one of those worksheets to the master worksheet.

Cheers!
I think its working on now! It must be my data! Cheers
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top