Macro to Open workbooks and copy data to respective sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,509
Office Version
  1. 2021
Platform
  1. Windows
I have written code to open two workbooks and to copy the data to sheet(2) (New Inventory) and to sheet(3) (Used Inventory)

The macro allows me to select the workbooks to be copied, however, new worksheets are created and the data copied into the new sheets

1) I want the data from the first file selected to be copied to sheet(2) "New Inventory"
2) I want the data from the second file selected to be copied to sheet(3) "Used Inventory"


The source data only has one sheet


It would be appreciated if someone would kindly assist me in amending the code so that the data is copied into the correct sheets



Code:
 Sub Open_Workbook()
ChDir ("C:\downloads")
With Sheets(2)
.Range("A1:DZ5000").ClearContents
End With


Dim nb As Workbook, tw As Workbook, ts As Worksheet
A = Application.GetOpenFilename
If A = False Or IsEmpty(A) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(A)


nb.Sheets(1).Copy Workbooks("Overaged Stock Report.xlsm").Sheets(2)

ChDir ("C:\downloads")
With Sheets(3)
.Range("A1:DZ5000").ClearContents

End With

A = Application.GetOpenFilename
If A = False Or IsEmpty(A) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(A)

nb.Sheets(1).Copy Workbooks("Overaged Stock Report.xlsm").Sheets(3)

ChDir ("C:\my Documents")

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,203,487
Messages
6,055,713
Members
444,810
Latest member
ExcelMuch

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