Formula for Copying Specific Data to a New Sheet

RiRi ChickenPie

New Member
Joined
May 31, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello world,

I would like to create a formula that copies all the data from rows which contain "00" in the last two digits of column L, to a new sheet (such as the cells highlighted in yellow). I know I could sort column L by "00" in the last two digits and then copy/paste, but I am trying to learn some shortcuts to streamline the process across multiple products. I hope this description was concise enough. Thank you for any help!

-RiRi
 

Attachments

  • Error Log Capture.PNG
    Error Log Capture.PNG
    41 KB · Views: 25

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the Board!
It's always best if you provide a copy of your sheet using the XL2BB - Excel Range to BBCode otherwise we have to create your sheet to test any code. Also, the following information is missing from your image & description: Does the data start in row 2 (with headers in row 1); what's the name of the sheet; do you want to copy the filtered data to another sheet in the same workbook, and if so - to what row; how many columns do you want copied (the image only shows to column P - is that the last column); what about the second instance of 68B00 that you didn't highlight in yellow; do you want to exclude any values that end with 3 or more zeroes; is the data in any column the result of formulas or is it all constant values; etc?

Having said that, the following demonstrates one way of achieving what I think you want - just change the sheet names to suit. Try it on a copy of your data & let me know how you go (y) :)
VBA Code:
Option Explicit
Sub CopyLast2_00()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")          '<~~ *** Change to actual sheet names ***
    Set ws2 = Worksheets("Sheet2")
    Dim a, i As Long, r As Range
    a = ws1.Range("L2:L" & Cells(Rows.Count, "L").End(xlUp).Row)
    
    For i = LBound(a, 1) To UBound(a, 1)
        If Right(a(i, 1), 2) = "00" And Mid(a(i, 1), Len(a(i, 1)) - 2, 1) <> "0" Then
            If r Is Nothing Then
                Set r = ws1.Cells(i + 1, 1).EntireRow
            Else
                Set r = Union(r, ws1.Cells(i + 1, 1).EntireRow)
            End If
        End If
    Next i
    If Not r Is Nothing Then
        r.Copy ws2.Range("A" & ws2.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1)
    Else
        MsgBox "No records meet the criteria"
    End If
End Sub
 
Upvote 0
Maybe with the filter formula:
Book1
ABCDEFGHIJKLMNO
1BO02AA40128YesPMcBF231301316231371440169.411EPTYWHGcc4221607160
2BO02AA40128YesNMCMc2313011352313013161713KAOYWHGcc231307711
3BO02AA40128YesPMcBF231211445231301135212.811EPTYWHGcc4221607160
4BO02AA40128YesNMCMc2312110402312114454168800YWHGcc230958128
5BO02AA40128YesPMcBF23121951231211040846K00YWHGcc4221607160
6BO02AA40328YesNMCMD23135118231391133106.311AA0YWHGcc231340001
7BO02AA40328YesPMcMG2313119322313511877811FVCYWHGcc4231317169
8
9The below would be on another sheet
10BO02AA40128YesNMCMc2312110402312114454168800YWHGcc230958128
11BO02AA40128YesPMcBF23121951231211040846K00YWHGcc4221607160
Sheet1
Cell Formulas
RangeFormula
A10:O11A10=FILTER(Sheet1!A1:O7,RIGHT(Sheet1!L1:L7,2)="00")
Dynamic array formulas.
 
Upvote 0
Welcome to the Board!
It's always best if you provide a copy of your sheet using the XL2BB - Excel Range to BBCode otherwise we have to create your sheet to test any code. Also, the following information is missing from your image & description: Does the data start in row 2 (with headers in row 1); what's the name of the sheet; do you want to copy the filtered data to another sheet in the same workbook, and if so - to what row; how many columns do you want copied (the image only shows to column P - is that the last column); what about the second instance of 68B00 that you didn't highlight in yellow; do you want to exclude any values that end with 3 or more zeroes; is the data in any column the result of formulas or is it all constant values; etc?

Having said that, the following demonstrates one way of achieving what I think you want - just change the sheet names to suit. Try it on a copy of your data & let me know how you go (y) :)
VBA Code:
Option Explicit
Sub CopyLast2_00()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")          '<~~ *** Change to actual sheet names ***
    Set ws2 = Worksheets("Sheet2")
    Dim a, i As Long, r As Range
    a = ws1.Range("L2:L" & Cells(Rows.Count, "L").End(xlUp).Row)
   
    For i = LBound(a, 1) To UBound(a, 1)
        If Right(a(i, 1), 2) = "00" And Mid(a(i, 1), Len(a(i, 1)) - 2, 1) <> "0" Then
            If r Is Nothing Then
                Set r = ws1.Cells(i + 1, 1).EntireRow
            Else
                Set r = Union(r, ws1.Cells(i + 1, 1).EntireRow)
            End If
        End If
    Next i
    If Not r Is Nothing Then
        r.Copy ws2.Range("A" & ws2.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1)
    Else
        MsgBox "No records meet the criteria"
    End If
End Sub
Thanks a ton for your reply! Very informative. I'll be sure to improve my posts in the future. Spending a little time this morning seeing if your formula works!
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,118
Members
453,021
Latest member
Justyna P

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