Find last row of one workbook and copy it to the last row of another workbook from column C

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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
if you know you want to copy columns A to L why not just set lastcol to 12 ie:
change:
VBA Code:
 lastCol = s1Sheet.Cells(3, Columns.Count).End(xlToLeft).Column 'Headers in Row 1
to
VBA Code:
 lastCol = 12
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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