Code to copy and paste all data from specified workbook

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
I need to first clear all worksheets in active workbook.
Then use the listbox item to open a new specified Workbook.
Then copy all the sheets paste to original Workbook then close the workbook down.
Keeping the original workbook open.

Code:
Option Explicit
Sub CopySheets()

Dim AutomatedCardworkerWorkbook As Workbook
Dim JobCardMasterWorkbook As Workbook
Dim JobCardMasterWorksheets As Worksheet
Dim Ws As Worksheet
Dim wb As Workbook
Dim strArray As String

For counter = 0 To ListBox3.Items.Count - 1
strArray(counter) = ListBox3.Items(counter)


Set wb = Workbooks(ListBox3.Value)
Set AutomatedCardworkerWorkbook = Workbooks("Automated Cardworker.xlsm")
Set JobCardMasterWorkbook = strArray
Set JobCardMasterWorksheets = JobCardMasterWorkbook.Sheets

Application.DisplayAlerts = False

AutomatedCardworkerWorkbook.Sheets("Job Card Master").Delete
AutomatedCardworkerWorkbook.Sheets("Job Card with Time Analysis").Delete
AutomatedCardworkerWorkbook.Sheets("Check Sheet").Delete
AutomatedCardworkerWorkbook.Sheets("SPEC SHEET").Delete
AutomatedCardworkerWorkbook.Sheets("Electrical inspection").Delete
AutomatedCardworkerWorkbook.Sheets("PRE ASSEMBLY3").Delete
AutomatedCardworkerWorkbook.Sheets("TOOLPOD").Delete
AutomatedCardworkerWorkbook.Sheets("FAB SHOP").Delete
AutomatedCardworkerWorkbook.Sheets("STOCK LIST").Delete
AutomatedCardworkerWorkbook.Sheets("ORDER LIST").Delete
AutomatedCardworkerWorkbook.Sheets("PRE-DEL").Delete
AutomatedCardworkerWorkbook.Sheets("WOOD SHOP").Delete
AutomatedCardworkerWorkbook.Sheets("Electrical load analysis").Delete

For Each Ws In JobCardMasterWorkbook.Sheets
   Ws.Copy After:=AutomatedCardworkerWorkbook.Sheets(AutomatedCardworkerWorkbook.Sheets.Count)
 
Next Ws
Application.DisplayAlerts = True

End Sub
 
Last edited by a moderator:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
OK, I've updated your thread title, added code tags and added your comments to your original post. :)

So, a couple of questions:

- is the workbook with the code in it the "Automated Cardworker.xlsm" workbook?
- is the listbox multi-select or single?
 
Upvote 0
OK, I've updated your thread title, added code tags and added your comments to your original post. :)

So, a couple of questions:

- is the workbook with the code in it the "Automated Cardworker.xlsm" workbook? Yes correct
- is the listbox multi-select or single? Single
Thanks
 
Upvote 0
What exactly is in listbox3? The name of the wb or the whole file path? Are the listbox3 wbs in the same folder as the active wb? Dave
 
Upvote 0
What exactly is in listbox3? The name of the wb or the whole file path? Are the listbox3 wbs in the same folder as the active wb? Dave
Sorry this is a mess it's just the name of the workbooks i think the file path should be there. The list box workbooks are in a separate folder.
 
Upvote 0
Well what's your plan? U can't get info from a wb file if U don't specify the entire path. U could put the whole path(s) in the listbox, or the whole path(s) in a worksheet, or hard code the path(s) in vba, or search all directories for a "fuzzy" match which may crash dependent upon how unique the wb names are. I'm guessing that using a blank column to list entire wb filepaths and then loading only the wb names to your listbox would be easiest to manage. Can't really code anything until U decide. HTH. Dave
 
Upvote 0
Sorry Darren... after a re-read of your objective, you want to delete all of your sheets... it seems like that wouldn't work. How are U loading your listbox to begin with anyways? Dave
 
Upvote 0
Sorry Darren... after a re-read of your objective, you want to delete all of your sheets... it seems like that wouldn't work. How are U loading your listbox to begin with anyways? Dave
I load it from additem in my code.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,618
Members
449,238
Latest member
wcbyers

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