Avatar of fb1990
fb1990
 asked on

Extract Data based on Criteria

Hello Experts,

Can someone please help me with a Excel VBA code to extract the values in columns A & B in the Data tab to the Data1 tab and place the data in Columns A & B starting in A3 with the header in A2.  

I want the data extracted if the values in Column H of the Data Tab are Account OR C.O.D.

VBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
Martin Liss

In your sample there is a Data2 sheet but no Data1 sheet. The Data2 sheet does not show any dates,
fb1990

ASKER
@Martin Liss . Very sorry, I want result in Data2
Martin Liss

Change row 6 if you want to put the date in a different column.
Sub TransferData()
Dim lngLastRow As Long
Dim lngRow As Long
Dim wsData As Worksheet
Dim wsDest As Worksheet
Const DATE_COL = "C"

Set wsData = ThisWorkbook.Worksheets("Data")
Set wsDest = ThisWorkbook.Worksheets("Data2")

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
wsDest.UsedRange.Cells.Offset(1, 0).ClearContents
With wsData
    lngLastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For lngRow = 2 To lngLastRow
        .Range("A" & lngRow & ":B" & lngRow).Copy wsDest.Range("A" & lngRow + 1)
        Select Case .Range("H" & lngRow)
            Case "Account", "C.O.D."
            .Range("H" & lngRow).Copy wsDest.Range(DATE_COL & lngRow + 1)
        End Select
    Next
End With

wsDest.Activate
With Application
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Martin Liss

In this workbook press Ctrl+Shift+T
29233237.xlsm
Martin Liss

The workbook I posted shows this; no P.O's.
2022-02-09_11-09-35.pngIf you removed column C then change the two H's in this portion of the code to G's
        Select Case .Range("H" & lngRow)
            Case "Account", "C.O.D."
            .Range("H" & lngRow).Copy wsDest.Range(DATE_COL & lngRow + 1)
        End Select

Open in new window


If you are still having a problem then please attach your workbook.
fb1990

ASKER
@Martin Liss Thanks.  It does not show P.O, but i don't want the data in Data2.  I need to see data where values in column H is Account or C.O.D. While everything else is filterd out.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Martin Liss

Please attach your workbook.
ASKER CERTIFIED SOLUTION
Martin Liss

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
fb1990

ASKER
This is exactly what I wanted.  Thanks Martin Liss for a an excellent solution.  I really appreciated your help.  Thank you so much for staying with me...
Martin Liss

You’re welcome and I’m glad I was able to help. And thank you for the testimonial.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015 and 2017
              Experts Exchange Distinguished Expert in Excel 2018 and 2021
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2020
              Experts Exchange Top Expert VBA 2018 to 2020
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy