Forum Discussion

JB_SE_ED's avatar
JB_SE_ED
Copper Contributor
Feb 27, 2023

Need help with a VBA Macro for copying and pasting of rows.

Hello, 

 

My experience is fairly limited with Macro's, and even more so with VBA code (so please feel free to be as specific as possible). But I am building an automation flow that uses macro's in various parts of the process. I have managed to troubleshoot and fix the macros in other parts of the flow, all except for this one.

 

This macro's purpose is to;

  1. Filter the data set based on the number in column "F"
  2. Select columns "A:F" and copy all rows of the resulting data set
  3. Remove all filters
  4. paste the rows that were just copied, at the first empty row at the bottom of the data set.

 

Steps 1-3 are working perfectly, but when the code gets to step 4, the paste function is not working.

 

I have researched and altered the code in several different ways with no success. One thing I find odd, is that the code I am using now will work if I paste into the last row that contains data, but not when I try to paste into the first empty row (the row right below). 

 

 This is the code i'm using

 

This is the worksheet with the filter applied

 

Please tell me what i'm doing wrong.

 

Thanks!

  • JB_SE_ED 

    Thank you, I downloaded the workbook. Try this version:

    Sub Sample()
        Dim LastRow As Long
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        Range("F1:F" & LastRow).AutoFilter Field:=1, Criteria1:="2"
        Range("A2:F" & LastRow).Copy Destination:=Range("A" & LastRow + 1)
        Range("F1:F" & LastRow).AutoFilter Field:=1
    End Sub
  • JB_SE_ED 

    Could you attach a small sample workbook demonstrating the problem (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar?