VBA Script - Copy all the data from one workbook to another workbook

fahadalambd

New Member
Joined
Sep 16, 2022
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
Good Afternoon everyone,

I need to copy all the data from one workbook (Test1.xlsx) to another workbook (Test2.xlsm).
There are so many sheets in Test1.xlsx file but I need to copy only "Sheet1".
So far, I tried the below code but it is not working :(

VBA Code:
Sub move_data()
    Workbooks("Test1.xlsx").Worksheets("Sheet1").Range("A1").Copy _
    Workbooks("Test2.xlsm").Worksheets("Sheet2").Range("A2")
End Sub

Please help me if you know the solution. :)
Thanks in advance
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code:
.Range("A1").Copy
That only copies cell A1.
 
Upvote 0
Both workbooks need to be open.
Code can be run from either workbook.
Change references to meaninful names and change as required.
Code:
Sub Maybe()
Dim wb1 As Workbook, wb2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet

Set wb1 = Workbooks("Book11111.xlsm")    '<----- Workbook is open. Change name as required.
Set wb2 = Workbooks("Book22222.xlsm")    '<----- Workbook is open. Change name as required.

Set sh1 = wb1.Sheets("Sheet1")    '<---- Change reference as required
Set sh2 = wb2.Sheets("Sheet1")    '<---- Change reference as required

Application.ScreenUpdating = False

sh1.Cells(1, 1).Resize(sh1.Cells(Rows.Count, 1).End(xlUp).Row, sh1.UsedRange.Columns.Count).Copy sh2.Cells(2, 1)    '<---- Pastes in A2 on down/right

Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Both workbooks need to be open.
Code can be run from either workbook.
Change references to meaninful names and change as required.
Code:
Sub Maybe()
Dim wb1 As Workbook, wb2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet

Set wb1 = Workbooks("Book11111.xlsm")    '<----- Workbook is open. Change name as required.
Set wb2 = Workbooks("Book22222.xlsm")    '<----- Workbook is open. Change name as required.

Set sh1 = wb1.Sheets("Sheet1")    '<---- Change reference as required
Set sh2 = wb2.Sheets("Sheet1")    '<---- Change reference as required

Application.ScreenUpdating = False

sh1.Cells(1, 1).Resize(sh1.Cells(Rows.Count, 1).End(xlUp).Row, sh1.UsedRange.Columns.Count).Copy sh2.Cells(2, 1)    '<---- Pastes in A2 on down/right

Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Thank you so much mate. This code is working perfectly :)
 
Upvote 0
Thank you for letting us know and enjoy the upcoming festive holidays.
 
Upvote 0

Forum statistics

Threads
1,224,738
Messages
6,180,673
Members
452,993
Latest member
FDARYABEE

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