VBA Code to Copy / Paste efficiently and re-apply

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
226
Office Version
  1. 365
Platform
  1. Windows
I posted in an earlier thread and found some answers however I still lack some finishing touches to the code to complete the task i would like to do.
Macro I would like to achieve is this
Copy Rows based on matching Criteria (so far i have that working)
Paste rows as values in another sheet (also seems to work)
Stops looking to copy and paste when it gets to the bottom rows of data on Sheet 1 that are Vlookups with no value (#N/A) (this is where i struggle)

I would like the pasting on the new sheet to always start in a specific row so that the headers aren't copied over
I would like when i rerun the macro for the previous pasted data to be cleared and then new updated data inserted.

Below is sample of my code that I crowdsourced from google. Ideally I wanted to Tie this macro to a shape button so over time as we click it it will reupdate "Sheet2"

Thank you readers for your time!


VBA Code:
Sub Filter_Data()
lastrow = Worksheets("sheet1").Range("A" & Rows.count).End(xlUp).Row

For r = 6 To lastrow
 If Not Error("Sheet1").Cells("G") Then
    If Worksheets("Sheet1").Range("D" & r).Value = "0101-6" And Worksheets("Sheet1").Range("G" & r).Value <> "Closed-Cancelled" And Worksheets("Sheet1").Range("G" & r).Value <> "Closed - LTCM Effective" And Worksheets("Sheet1").Range("G" & r).Value <> "Closed - LTCM Ineffective" And Worksheets("Sheet1").Range("G" & r).Value <> "Closed-No Response Required" And Worksheets("Sheet1").Range("G" & r).Value <> "#N/A" Then
      Worksheets("Sheet1").Rows(r).Copy
      Worksheets("Sheet2").Activate
      lastrowaact = Worksheets("Sheet2").Range("A" & Rows.count).End(xlUp).Row
      Worksheets("Sheet2").Range("A" & lastrowaact + 1).PasteSpecial Paste:=xlPasteValues
        
    End If
 End If
 
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of Sheet1. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Thank you for the advice. Let me make a mock sample set of sheet one in a separate copy of the xlsm. we have alot of information thats proprietary but i will pare it down the information into a sampling of data and attach it using XL2BB
 
Upvote 0
Please attach a screenshot not a picture.
 
Upvote 0
Okay i hope this works I am attaching "Sheet 1"

Customer Database Test for mrxcel.xlsm
ABCDEFGHIJKLMNOPQRSTUV
5QIMS#Doc TypeRankSupplier CodePart NamePart NumberOverall StatusNCD DescriptionModelLT CM Plan DueOriginal LTCM Actual Due DateRevised LTCM Actual Due DateCloseInitial Issuance DateOfficial Issuance DateLTCM Plan SubmittedLTCM Plan Accepted DateLTCM Plan Rejection DateLTCM Actual SubmittedLTCM Actual Accepted DateLTCMActualRejectionDateNAMC
601-01016-V6-4053QPRB0101-6Part 111122-33444Closed-CancelledFIT CONDITIONV6 5/3/214/28/21 Cust 1
701-01016-V6-4054QPRB0101-6Part 111122-33444Closed-CancelledLOOSEV6 5/3/214/28/21 Cust 1
801-01016-V6-4055QPRC0101-6Part 111122-33444Closed-CancelledPOPPING / PULLING OUTV6 5/3/214/28/21 Cust 1
901-01016-V6-4086QPRB0101-6Part 211122-33444Closed-CancelledWRONG PARTV6 3/8/223/7/22 Cust 1
1014-01016-ZR-4093QPRB0101-6Part 322233-44555Awaiting Official, STCM ApprovedFOREIGN MATERIALI4 3/31/22 Cust 2
1114-01016-ZR-4094QPRB0101-6Part 322233-44555Awaiting Official, STCM ApprovedCRACKEDI4 4/1/22 Cust 2
1215-01022-M20-4009QPRB0102-2Part 411122-33444Awaiting Official, STCM ApprovedLEAKSI4 3/31/22 Cust 3
1301-01016-T1-4027QPRB0101-6Part 6011122-33444Officially Released, Awaiting LTCM PlanABNORMAL NOISEV64/19/22 4/1/224/4/22 Cust 1
1415-01022-M20-4008QPRB0102-2part 411122-33444Officially Released, Awaiting LTCM PlanOtherI44/17/22 3/31/224/2/22 Cust 3
1514-01017-TNGA-4016QIRC0101-7Part 122233-44555Officially Released, Awaiting LTCM PlanFOREIGN MATERIALI44/14/22 3/29/223/30/22 Cust 2
1614-01016-TNGA-4098QPRB0101-6Part 522233-44555Officially Released, Awaiting LTCM PlanEXCESS MATERIALI44/12/22 3/23/223/28/22 Cust 2
1701-01016-V6-4090QIRB0101-6Part 222233-44555LTCM Plan Accepted, Awaiting LTCM ActualADHESIVE NGV64/7/224/15/22 3/22/223/23/223/31/224/4/22 Cust 1
1801-01022-T2-4019QPRC0102-2Part 122233-44555LTCM Plan Accepted, Awaiting LTCM ActualAPPEARANCE DEFECTI44/1/224/8/22 3/15/223/17/223/31/224/4/22 Cust 1
1914-01016-GR-4192QPRB0101-6Part 222233-44555LTCM Plan Accepted, Awaiting LTCM ActualMISSING PARTV64/1/224/15/22 3/16/223/17/223/31/224/1/22 Cust 2
2001-01016-V6-4089QIRB0101-6Part 222233-44555LTCM Plan Accepted, Awaiting LTCM ActualNOT SEATEDV63/29/224/1/22 3/11/223/14/223/30/223/31/22 Cust 1
2115-01016-V6T-4002QIRC0101-6part 7022233-44555Awaiting LTCM Plan Response Acceptance, LTCM Plan SubmittedBURRSV6T3/23/225/31/22 2/11/223/8/223/23/22 Cust 3
2214-01016-T24-4001QIRB0101-6Part 222233-44555Closed - LTCM EffectiveMISSING SUB-COMPONENTI43/8/222/22/22 2/28/222/20/222/21/22 2/22/22 Cust 2
2314-01016-GR-4187QPRC0101-6Part 222233-44555Closed - LTCM EffectiveNOT SEATEDV62/26/223/11/22 4/3/222/10/222/11/222/25/223/2/22 3/30/22 Cust 2
2415-01016-A25-4027QPRC0101-6Part 222233-44555LTCM Plan Accepted, Awaiting LTCM ActualMACHINING DEFECTI42/23/223/31/22 1/5/222/8/222/23/222/25/22 Cust 3
2514-01017-TNGA-4015QPRB0101-7Part 822233-44555Closed - LTCM EffectiveBROKEN / BREAKINGI42/22/222/25/22 3/13/222/7/222/7/222/21/222/22/22 3/8/22 Cust 2
2614-01016-TNGA-4095QPRB0101-6Part 922233-44555LTCM Plan Accepted, Awaiting LTCM ActualMACHINING DEFECTI42/16/222/28/22 1/27/222/1/222/15/222/17/22 Cust 2
2715-01016-2GR-4111QPRC0101-6Part 1022233-44555LTCM Actual Accepted, Awaiting LTCM ConfirmationNOT TAPPEDV62/11/223/7/22 1/20/221/27/222/10/223/2/22 3/23/22 Cust 3
2814-01022-TNGA-4037QPRC0102-2Part 122233-44555Closed - LTCM EffectiveFLASHI41/29/222/11/22 2/15/221/13/221/14/222/8/222/9/22 2/9/22 Cust 2
2915-01016-M20-4015QPRB0101-6Part 322233-44555LTCM Plan Accepted, Awaiting LTCM ActualOtherI41/29/222/25/22 1/13/221/14/222/7/222/15/22 Cust 3
3044-01016-SRVC-4017QIRB0101-6Part 522233-44555Closed-No Response RequiredDealer ClaimV61/21/22 1/21/221/6/221/6/22 Cust 4
3115-01016-M20-4014QPRC0101-6Part 822233-44555LTCM Plan Accepted, Awaiting LTCM ActualOtherI41/6/222/25/22 12/21/2112/22/212/7/222/8/22 Cust 3
Sheet1
 
Upvote 0
in my haste to show a sampling of data i wanted to copy over to another sheet i eliminated the cells below that are formulas which are referencing blank cells in a master file that show up as "#N/A" because the result is blank.

Trying to figure out how to ignore such errors
 
Upvote 0
Make sure that you have headers in row 1 of Sheet2 and then try this macro:
VBA Code:
Sub Filter_Data()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    desWS.UsedRange.Offset(1).ClearContents
    With srcWS
        .Range("W2:W" & LastRow).Formula = "=IF(AND(D2=""0101-6"",G2<>""Closed-Cancelled"",G2<>""Closed - LTCM Effective"",G2<>""Closed - LTCM Ineffective"",G2<>""Closed-No Response Required"",G2<>""#N/A""),""true"",""false"")"
        .Cells(1).CurrentRegion.AutoFilter 23, "true"
        .Range("A2:V" & LastRow).SpecialCells(xlCellTypeVisible).Copy
        With desWS
            .Cells(.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Columns.AutoFit
        End With
        .Range("A1").AutoFilter
        .Columns("W").Delete
    End With
    Application.ScreenUpdating = True
End Sub
The macro use a helper column (column W) so I assume that column W will not be used.
 
Upvote 0
Solution
Gave it a test recieved Run time Error 1004 Autofilter method of Range class failed. Debug highlighted

.Cells(1).CurrentRegion.Autofilter 23, "True"
 
Upvote 0
I tested the macro on the data you posted and it worked properly. Are you using the macro on data that is different from the data you posted?
 
Upvote 0
AH! i forgot to shift the columns so the header was on Row 1. i did that and it worked! Thank you. however i was hoping to reserve space above my for buttons but i guess i could just widen Row 1 itself to make white space =)
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

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