Copying from last row column c from one workbook to another

dimsums

New Member
Joined
Jun 29, 2020
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi Guys, I have a code below that works by finding the last row of the source file and paste it into the destination sheet of another workbook, all the data is stored from Column C to L. the transfer works fine but only Transfers C to J the misses out the last two cells. Can someone help me out on this
Sub LastRowToExport1()

Dim lastS1Row As Long 'Last Source Row
Dim nextS2Row As Long 'Next Target Row
Dim lastCol As Long 'Last Column on Source Sheet
Dim s1Sheet As Worksheet, s2Sheet As Worksheet
Dim source As String 'Source worksheet name
Dim target As String 'Target worksheet name
Dim path As String

'---SET SHEET NAMES HERE---
source = "Sheet2" 'Source Worksheet Name
path = "T:\ROC-IT PROGAM\OFI Management\OFI Register.xlsm" 'Target File Path including file name and extension
target = "ALL OFIs" 'Target Worksheet Name

'WARNING - THIS LINE WILL DISABLE NORMAL BREAKING - IN CASE OF LOOP
Application.EnableCancelKey = xlDisabled 'Disables breaking when opening new book

'Define worksheets
Set s1Sheet = ThisWorkbook.Sheets(source) 'Source Sheet
Set s2Sheet = Workbooks.Open(path).Sheets(target) 'Target Sheet

'Get the last row on each sheet and set the NEXT Row on the target. Also total columns.
lastS1Row = s1Sheet.Range("C" & Rows.Count).End(xlUp).Row
nextS2Row = s2Sheet.Range("C" & Rows.Count).End(xlUp).Row + 1
lastCol = s1Sheet.Cells(3, Columns.Count).End(xlToLeft).Column 'Headers in Row 1

'---COPY ENTIRE ROW--- Loop through Cells by column
For lCol = 1 To lastCol
s2Sheet.Cells(nextS2Row, lCol) = s1Sheet.Cells(lastS1Row, lCol)
Next lCol

'WRAP UP, SAVE EXPORTED SHEET, REACTIVATE SOURCE SHEET
s2Sheet.Activate
ActiveWorkbook.Close SaveChanges:=True
s1Sheet.Activate

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Consider the following approach

VBA Code:
Sub LastRowToExport1()
  Dim sPath As String, wb2 As Workbook, sh2 As Worksheet
  
  sPath = "T:\ROC-IT PROGAM\OFI Management\OFI Register.xlsm" 'Target File Path including file name and extension
  Set wb2 = Workbooks.Open(sPath)
  Set sh2 = wb2.Sheets("ALL OFIs")    'Target Worksheet Name
  sh2.Range("C" & Rows.Count).End(3)(2).Resize(1, 10).Value = ThisWorkbook.Sheets("Sheet2").Range("C" & Rows.Count).End(3).Resize(1, 10).Value
  wb2.Close SaveChanges:=True
End Sub
 
Upvote 0
I just tried it now.
It works! I see that there are several ways of doing this, thank you so much!
 
Upvote 0
Im glad to help you, thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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