VBA to copy cell value from the last row of the workbook to another workbook

shernel07

New Member
Joined
Jun 19, 2019
Messages
8
Hi there,

I have two workbooks eg. Data.xlsx and Final.xlsx

In Data.xslx I have the sum of A2:A3 in cell A4

Now I need a macro code to copy the data from cell A4 and transfer it to the Final.xlsx workbook in cell A4 for example.

P.S I need to data to get transferred not the formula present in A4.

Can you guys help me in this? Thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Code:
Workbooks("Final.xlsx").Sheets("Sheet1").Range("A4") = Workbooks("Data.xlsx").Sheets("Sheet1").Range("A4")
 
Upvote 0
That cell A4 would always change. So I need a code which always selects the last row with value in column A.
 
Upvote 0
In workbook Data.xlsx , I want the cell value of the last row in Column A to be copied to the Final.xlsx workbook.

The last row would always vary
 
Upvote 0
Code:
Sub transfer()
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Workbooks("Final.xlsx").Sheets("Sheet1").Range("A4") = Workbooks("Data.xlsx").Sheets("Sheet1").Range("A" & LastRow)
End Sub

is that what you are looking for?
 
Upvote 0
Yes exactly what I was looking for but when I run it why does it show Subscript out of range??? Run time error 9.

I copy pasted the code in the Final.xlsx workbook.

Is there some location of the workbook that needs to be provided?
 
Upvote 0
Hello just one last favour : I want to add a button on the Final.xlsx sheet which pulls the data of the last row in column A in data.xlsx sheet to the Final.xlsx

The code you have provided sends data from Data.xlsx to the Final.xlsx which I do not want making the code to be present on the Data.xlsx workbook

I want the code to be on the Final.xlsx workbook where when i click the button the data on the last row of Data.xlsx reflects in Final.xlsx
 
Upvote 0
Code:
Sub transfer()
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long
Set wb = Workbooks("Data.xlsx")
Set ws = wb.Sheets("Sheet1")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Workbooks("Final.xlsx").Sheets("Sheet1").Range("A4") = Workbooks("Data.xlsx").Sheets("Sheet1").Range("A" & LastRow)
End Sub

place this in a standard module in Final. Assign macro to button/shape.

note that final.xlsx extension will change to final.xlsm (because you are adding code to the final sheet, so make that change in the code)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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