Feb 27 2023 06:00 AM
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;
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!
Feb 27 2023 06:26 AM
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?
Feb 27 2023 07:26 AM
@Hans Vogelaar I've attached a sample file below. There should only be one Macro in the workbook named "Sample". That's what i'm trying to run. Sample Workbook
Feb 27 2023 07:34 AM
I don't have access to your SharePoint. Can you share the file?
Feb 27 2023 07:52 AM
Feb 27 2023 08:43 AM
SolutionThank 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
Feb 27 2023 09:06 AM
Feb 27 2023 09:28 AM
Feb 27 2023 12:26 PM
Feb 27 2023 12:29 PM
Feb 27 2023 01:20 PM - edited Feb 27 2023 01:21 PM
There is probably a better way to do this with VSTACK etc.
(Wait - I made a mistake. I'll be back)
Feb 27 2023 01:26 PM
Try this:
Sub Sample()
Dim LastRow As Long
Dim Uniques
Dim OneValue
Dim NewLastRow As Long
Dim Idx As Long
Application.ScreenUpdating = False
LastRow = Range("A" & Rows.Count).End(xlUp).Row
NewLastRow = LastRow
Uniques = Application.Unique(Range("F2:F" & LastRow))
For Each OneValue In Uniques
If OneValue > 1 Then
Range("F1:F" & LastRow).AutoFilter Field:=1, Criteria1:=OneValue
For Idx = 1 To OneValue - 1
Range("A2:F" & LastRow).Copy Destination:=Range("A" & NewLastRow + 1)
NewLastRow = Range("A" & Rows.Count).End(xlUp).Row
Next Idx
End If
Next OneValue
Range("F1:F" & LastRow).AutoFilter Field:=1
Application.ScreenUpdating = True
End Sub
Feb 28 2023 06:16 AM