Hi all,
The code below is working just as I want it to apart for one issue which is it also copies the last row (Totals row), I do not want that.
The code was found on the net and adjusted to fit my needs. From item 5 onwards it is a copy of items 1 to 4.
I have researched how to fix this but everything I have tried either breaks the code or does nothing. I am sure it is a two second tweak but my near non-existent VBA code knowledge is stopping me from finding it
Any help would be appreciated
Thanks in advance.
The code below is working just as I want it to apart for one issue which is it also copies the last row (Totals row), I do not want that.
The code was found on the net and adjusted to fit my needs. From item 5 onwards it is a copy of items 1 to 4.
I have researched how to fix this but everything I have tried either breaks the code or does nothing. I am sure it is a two second tweak but my near non-existent VBA code knowledge is stopping me from finding it
Any help would be appreciated
Thanks in advance.
Code:
Sub Clear_Existing_Data_Before_Paste()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Set wsCopy = Workbooks("Alternative_Mess_Accounts.xlsx").Worksheets("Week_1_Stock_Sheet")
Set wsDest = Workbooks("CopyTestBook.xlsx").Worksheets("Sheet1")
'Code below is to Copy Column A:D from current worksheet to a new workbook A:D
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
'3. Clear contents of existing data range
wsDest.Range("A5:D" & lDestLastRow).ClearContents
'4. Copy & Paste Data - last row for original code
wsCopy.Range("A5:D" & lCopyLastRow).Copy _
wsDest.Range("A5")
'Code below is to Copy Column AI to J
'5. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'6. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
'7. Clear contents of existing data range
wsDest.Range("J5:J" & lDestLastRow).ClearContents
'8. Copy & Paste Data - last row for original code
wsCopy.Range("AI5:AI" & lCopyLastRow).Copy _
wsDest.Range("J5")
End Sub