VBA Code to copy data and automate tasks

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a workbook when I have downloaded and would like VBA code to open the source workbook and to copy the data into the destination workbook in the sheet next to the sheet name "Pivot Table"

Your assistance in this regard will be most appreciated
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Maybe:

Code:
Sub Howard()

    Workbooks.Open Filename:="D:\common\data\DESTINATION WORKBOOK.xls"
    Workbooks.Open Filename:="D:\common\data\SOURCE WORKBOOK.xls"
    
    Workbooks("SOURCE WORKBOOK.xls").Sheets("Sheet1").Copy After:=Workbooks("DESTINATION.xls").Sheets("Pivot Table")
    
End Sub
 
Upvote 0
Hi John

Thanks for the help. I have used your code and adapted it to some code I have previously use. When activating the macro, I allow the user to select a source workbook, but the following code is highlighted


nb.Sheets("Sheet1").Copy After:=Workbooks("BR1 Vat Report Macro.xlsm").Sheets("Pivot Table")


See full code below

Sub Open_Workbook()

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("Sheet1").Copy After:=Workbooks("BR1 Vat Report Macro.xlsm").Sheets("Pivot Table")

End Sub

Your assistance in correcting this will be most appreciated
 
Upvote 0
Hi John

Thanks for the help. I have used your code and adapted it to some code I have previously use. When activating the macro, I allow the user to select a source workbook, but the following code is highlighted


nb.Sheets("Sheet1").Copy After:=Workbooks("BR1 Vat Report Macro.xlsm").Sheets("Pivot Table")


See full code below

Sub Open_Workbook()

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("Sheet1").Copy After:=Workbooks("BR1 Vat Report Macro.xlsm").Sheets("Pivot Table")

End Sub

Your assistance in correcting this will be most appreciated
 
Last edited:
Upvote 0
Hi John

Thanks for the reply. I am getting "run time error 9 subscript out of range" 7 the following code is highlighted

nb.Sheets("Sheet1").Copy After:=Workbooks("BR1 Vat Report Macro.xlsm").Sheets("Pivot Table")
 
Upvote 0
It means excel can't find something in the code.

Maybe you don't have a - nb.Sheets("Sheet1") ??

or you don't have a - Workbooks("BR1 Vat Report Macro.xlsm"). - This workbook needs to be open.

or you don't have a - Sheets("Pivot Table").

Check the spelling of the workbook and sheet names, and make sure there are no errant spaces.
 
Upvote 0
Hi John

Thanks for the reply. I did not have sheets("sheet1")

I have amended the code and it now works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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