copy and stack all tabs data to one sheet in another workbook using vba

NateD1

New Member
Joined
Apr 1, 2020
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hi All,
i have the below code i found which copys all data in multiple tabs in a workbook to one sheet.
would some one be able to adjust this code or simplifying so that it only copies particular tabs instead of all tabs. (i.e only Sheet1,Sheet3,Sheet4,Sheet5)
i need all this data then exported into another work book.

example of what ideally id like it to do, Main workbook where data to be copied to: workbook1, data where is stored and macro combines sheets: Workbook2
workbook 1, run macro > opens workbook 2 > combines all data on named tabs > exports combined stacked data to "Sheet1" on workbook1 > close Workbook2

VBA Code:
Sub Combine()
    Dim J As Integer
    Dim s As Worksheet

    On Error Resume Next
    Sheets(1).Select
    Worksheets.Add ' add a sheet in first place
    Sheets(1).Name = "Combined"

    ' copy headings
    Sheets(2).Activate
    Range("A1").EntireRow.Select
    Selection.Copy Destination:=Sheets(1).Range("A1")

    For Each s In ActiveWorkbook.Sheets
        If s.Name <> "Combined" Then
            Application.GoTo Sheets(s.Name).[a1]
            Selection.CurrentRegion.Select
            ' Don't copy the headings
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
            Selection.Copy Destination:=Sheets("Combined"). _
              Cells(Rows.Count, 1).End(xlUp)(2)
        End If
    Next
End Sub

thanks
 
Rich (BB code):
Sub Copy_Sheets_To_Master()
    Application.ScreenUpdating = False
    Dim wkbSource As Workbook, wsDest As Worksheet, ws As Worksheet, lRow As Long
    Set wsDest = ThisWorkbook.Sheets("Master")
    Set wkbSource = Workbooks.Open("Z:\Filelocation\" & Range("A1"))
    For Each ws In Sheets(Array("Sheet1", "Sheet3", "Sheet4", "Sheet5"))
        With ws
            lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Cells(2, 1).Resize(lRow - 1, 7).Copy wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Next ws
    wkbSource.Close False
    Application.ScreenUpdating = True
End Sub
Change the range (in red) to the cell that contains the file name. Make sure that the file name in that cell includes the extension, in your case xlsb. Also, when replying, please click the "Reply" button, not the "Reply With Quote" button. This helps to avoid clutter.
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
VBA Code:
Sub Copy_Sheets_To_Master()
    Application.ScreenUpdating = False
    Dim wkbSource As Workbook, wsDest As Worksheet, ws As Worksheet, lRow As Long
    Set wsDest = ThisWorkbook.Sheets("Master")
    Set wkbSource = Workbooks.Open("Z:\Filelocation\" & Range("A1"))
    For Each ws In Sheets(Array("Sheet1", "Sheet3", "Sheet4", "Sheet5"))
        With ws
            lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Cells(2, 1).Resize(lRow - 1, 7).Copy wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Next ws
    wkbSource.Close False
    Application.ScreenUpdating = True
End Sub

Regarding this code is there a way to copy all sheets based on tab names listed in another tab of the macro workbook? For example the list in a tab labeled "tabs" contains a list of tabs that I want copy and pasted: "Sheet1", "Sheet2", etc. instead of having to update it in the VBA screen every time there is a new updated tab?

Also I want a conditional copy/paste where if in my column labeled "Already Pasted" for each line of data with a string text has a "Yes" then we don't copy that line; we only copy/paste "No" so that we aren't duplicating any data transfers. Can this be done?

Thank you!
 
Upvote 0
VBA Code:
Sub Copy_Sheets_To_Master()
    Application.ScreenUpdating = False
    Dim wkbSource As Workbook, wsDest As Worksheet, ws As Worksheet, lRow As Long
    Set wsDest = ThisWorkbook.Sheets("Master")
    Set wkbSource = Workbooks.Open("Z:\Filelocation\" & Range("A1"))
    For Each ws In Sheets(Array("Sheet1", "Sheet3", "Sheet4", "Sheet5"))
        With ws
            lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Cells(2, 1).Resize(lRow - 1, 7).Copy wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Next ws
    wkbSource.Close False
    Application.ScreenUpdating = True
End Sub

Regarding this code is there a way to copy all sheets based on tab names listed in another tab of the macro workbook? For example the list in a tab labeled "tabs" contains a list of tabs that I want copy and pasted: "Sheet1", "Sheet2", etc. instead of having to update it in the VBA screen every time there is a new updated tab?

Also I want a conditional copy/paste where if in my column labeled "Already Pasted" for each line of data with a string text has a "Yes" then we don't copy that line; we only copy/paste "No" so that we aren't duplicating any data transfers. Can this be done?

Thank you!

Data file:
1650906636241.png


Tabs to copy paste:
1650906731186.png
 

Attachments

  • 1650906717157.png
    1650906717157.png
    11 KB · Views: 1
  • 1650906722930.png
    1650906722930.png
    11 KB · Views: 1
Upvote 0
Regarding this code is there a way to copy all sheets based on tab names listed in another tab of the macro workbook? For example the list in a tab labeled "tabs" contains a list of tabs that I want copy and pasted: "Sheet1", "Sheet2", etc. instead of having to update it in the VBA screen every time there is a new updated tab?

Also I want a conditional copy/paste where if in my column labeled "Already Pasted" for each line of data with a string text has a "Yes" then we don't copy that line; we only copy/paste "No" so that we aren't duplicating any data transfers. Can this be done?
Unless you have a question about the original question or solution posted (and aren't changing anything or introducing something new), it is usually much better to post your question to its own thread, than it is to dredge up an old thread. That way, it will appear in the "Unanswered threads" listing, which means it will get a lot more looks, and has a greater chance of being answered.

You can always include links to other threads in your question, if you feel that might be helpful.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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