Paste Set of Data onto last row

Nick70

Active Member
Joined
Aug 20, 2013
Messages
299
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have some code that takes some data in Sheet2 and copies it in Sheet1 on the row after the last one see below:

VBA Code:
Sub CopyAdditionaData()

'Copies data into Sheet1 from Sheet2

Dim Lr As Long, lc As Integer
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastRow As Long

Set wb = ThisWorkbook
Set ws1 = wb.Worksheets("Sheet2")
Set ws2 = wb.Worksheets("Sheet1")

ws1.Activate
Lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
Range("A3", Cells(Lr, lc)).Copy Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

End Sub

The problem I have is that there is a filter in Sheet1 so the last row is not the last row with data but the last VISIBLE row.
How can I change the code so that the data is pasted from Sheet2 to Sheet1 on the first empty row (so the one after the last row with data so ignoring filter)?

As a further example if I have data in Range(A2:C10) the last row with data will be row Range(A10:C10) but if I have a filter that excludes Range(A10:C10) then my last VISIBLE row with data will be Range(A9:C9) according to VBA code.
We should therefore remove filter from Sheet1, then the count rows, then apply filter again and copy data from Sheet2 to Sheet1.

Let me know in case not clear what I am trying to do.

Thanks,
N.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
adding this simple line should clear any filters for your active ws1 sheet.
then just reapply the filter.

VBA Code:
ws1.ShowAllData

or

you could just check to see if the next row is hidden.

VBA Code:
Do While Rows(Lr + 1).Hidden = True
    Lr = Lr + 1
Loop
 
Upvote 0
Solution

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

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