Select range that are auto-filtered and have certain text value

tmdgus

New Member
Joined
Oct 2, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi, I need some help with filtering and selecting specific cells with multiple criteria.
I have a table with a list of fruits and their expiry date.
I have tried using VBA to auto-filter the fruit data that are expiring in current month. (xlFilterThisMonth).
However, I only want to select data of apples that are expiring within a month, then copy and paste the selected data to sheet2.
Can anyone help me how to do so?
FruitList.xlsm
AB
1FruitExpiry Date
2Apple13/9/2022
3Banana15/11/2022
4Apple19/10/2022
5Citrus28/10/2022
6Banana30/9/2022
7Grape1/10/2022
8Apple11/10/2022
Sheet1
Cells with Data Validation
CellAllowCriteria
A2:A8ListApple,Banana,Citrus,Grape
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this:

VBA Code:
Sub data_expires_this_month()
  With Sheets("Sheet1")
    .Range("A1").AutoFilter Field:=2, Criteria1:=xlFilterThisMonth, Operator:=xlFilterDynamic
    .AutoFilter.Range.Copy Sheets("Sheet2").Range("A1")
    .ShowAllData
  End With
End Sub
 
Upvote 0
Hi Thank you for your help!
I tried with your code and it seems like the expiring data are copied and pasted well, but I want to copy and paste data only if the fruit is apple and it is expiring soon. Any suggestions on this?
Thank you!!
 
Upvote 0
VBA Code:
Sub Expiring_Apples()
    With Worksheets("Sheet1").Range("A1").CurrentRegion
        .AutoFilter 1, "Apple"
        .AutoFilter 2, 7, 11
        .Offset(1).Resize(.Rows.Count - 1).Copy Worksheets("Sheet2").Range("A1")
        .AutoFilter
    End With
End Sub
 
Upvote 0
VBA Code:
Sub Expiring_Apples()
    With Worksheets("Sheet1").Range("A1").CurrentRegion
        .AutoFilter 1, "Apple"
        .AutoFilter 2, 7, 11
        .Offset(1).Resize(.Rows.Count - 1).Copy Worksheets("Sheet2").Range("A1")
        .AutoFilter
    End With
End Sub
Thank you it worked !!
 
Upvote 0
...l, but I want to copy and paste data only if the fruit is apple ...

Try this:

VBA Code:
Sub data_expires_this_month()
  With Sheets("Sheet1")
    .Range("A1").AutoFilter 1, "Apple"
    .Range("A1").AutoFilter field:=2, Criteria1:=xlFilterThisMonth, Operator:=xlFilterDynamic
    .AutoFilter.Range.Copy Sheets("Sheet2").Range("A1")
    .ShowAllData
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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