VB Code to copy content of external worksheet to named worksheet in this workbook

tommill52

New Member
Joined
May 12, 2015
Messages
27
I am looking for some VB code for the following.
I need to copy all of the data (values only) from worksheet 'extworksheet' in workbook 'extworkbook.xlsx' to worksheet 'localworksheet' in this (active) workbook. Literally replicate the 'extworksheet' into worksheet 'localworksheet' from cell $A$1 on. The size of the 'extworksheet' can vary in rows and columns on a day to day basis, so I need something that copies all of the populated rows (ie row 1 to last row).
Can anyone help with this please? I have tried various code snippets I have found but they all come up with errors. I am using Office 365 (Excel 2016).
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hello tommill,

This should work for you.

Code:
Sub CopyPasteAll()

Dim lastRow As Long
Dim firstRow As Long
Dim lastColumn As Long
Dim firstColumn As Long

'delete current content in sheet 2
sheets("localworksheet").activate
cells.select
selection.delete

Sheets("extworksheet").Activate

'get last row of the data
lastRow = Cells(Rows.Count, 1).End(xlUp).Row 'use 1 if data starts in column 1, 2 for b etc..

'set first row of data
firstRow = 1

'set first column of data
firstColumn = 1

'get last column of data
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column 'use 1 if data starts in first row, 2 for second etc.. 

'Copy the data
Range(Cells(fr, fc), Cells(lr, lc)).Delete

'activate sheet 2 & paste values
sheets("localworksheet").activate
cells(1,1).PasteSpecial xlPasteValues

end sub

Tell me if this works for you :)
 
Last edited:
Upvote 0
Thanks for your reply. Am I missing something (not a VB expert), I can see how you activate the external worksheet, but where is the external workbook selected?
 
Upvote 0
I thought you had 2 sheets in 1 workbook.

If you have 2 workbooks, open the other with something like this:

Code:
'Open excelfile
Workbooks.Open Filename:= "your path/extworksheet.xlsx" , UpdateLinks:=0

I use the following code to activate files (I'm also still learning :) ) :

Code:
Windows("extworksheet.xlsx").Activate

Hope you can tweak the macro with this info.

Let us know if you need any more help.
 
Upvote 0
I thought you had 2 sheets in 1 workbook.

If you have 2 workbooks, open the other with something like this:

Code:
'Open excelfile
Workbooks.Open Filename:= "your path/extworksheet.xlsx" , UpdateLinks:=0

I use the following code to activate files (I'm also still learning :) ) :

Code:
Windows("extworksheet.xlsx").Activate

Hope you can tweak the macro with this info.

Let us know if you need any more help.

Thanks for your help. I was testing your code and having a few problems with it (my fault, I think) when somebody else provided me with a set of working.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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