Auto Filter

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,059
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this code and not sure its not selecting the data,

screen shot is attached, not sure as what is the actula problem.


VBA Code:
Sub AutoData()

    
  Dim lRow2 As Long
  Dim lColumn2 As Long

  lRow2 = Range("A1").End(xlDown).Row
  lColumn2 = Range("A1").End(xlToRight).Column
        
  Cells(lRow2, lColumn2).Select
    
    Selection.AutoFilter
    Range("A1").Select
    ActiveSheet.Range("A1" & lRow2).AutoFilter Field:=1, Criteria1:=Range("AU1").Value
       
    Range("A1:AS" & lRow2).SpecialCells(xlCellTypeVisible).Delete
    Selection.AutoFilter
    
End Sub
[ATTACH type="full"]97901[/ATTACH][ATTACH type="full"]97902[/ATTACH]
 

Attachments

  • 1693250260972.png
    1693250260972.png
    27.2 KB · Views: 10
  • 1693250276195.png
    1693250276195.png
    2.7 KB · Views: 10

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this:

VBA Code:
Sub AutoData()
  Dim lr As Long
  Dim lc As Long

  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  lr = Range("A" & Rows.Count).End(xlUp).Row
  lc = Range("A1").End(xlToRight).Column
        
  ActiveSheet.Range("A1", Cells(lr, lc)).AutoFilter Field:=1, Operator:= _
        xlFilterValues, Criteria2:=Array(0, Format(Range("AU1").Value, "mm/dd/yyyy"))
       
  Range("A2:AS" & lr).SpecialCells(xlCellTypeVisible).Delete
  Selection.AutoFilter
    
End Sub
 
Upvote 0
Solution
@DanteAmor

With the data set below
Book1.xlsb
ABCDEFGHIATAU
1Order DateSiteSP NameCategory NameShip ViaQuantityOrder Total24-Aug-2023
218-Feb-2014Off SiteJanet LeverlingSeafoodRoad912094.351
310-Mar-2014On SiteJanet LeverlingDairy ProductsSea491460.7846
405-Sep-2013Off SiteMargaret PeacockCondimentsRoad701400.661
527-Nov-2013On SiteLaura CallahanConfectionsSea6151.8594
614-Oct-2013Off SiteMichael SuyamaConfectionsSea24354.2892
706-Jun-2013Off SiteLaura CallahanSeafoodSea20632.073
812-Feb-2014On SiteMargaret PeacockCondimentsSea12377.2716
921-May-2013On SiteLaura CallahanGrains/CerealsSea70665.3838
1007-Aug-2013On SiteLaura CallahanProduceRoad21667.3902
1111-Feb-2013On SiteJanet LeverlingDairy ProductsRail30629.2116
1225-Apr-2013On SiteMargaret PeacockCondimentsSea9448.248
1325-Nov-2012Off SiteJanet LeverlingMeat/PoultrySea24270.9096
1401-May-2013On SiteNancy DavolioBeveragesRoad603425.4606
1523-Aug-2023On SiteAnne DodsworthCondimentsAir301456.6692
1604-Feb-2013On SiteLaura CallahanGrains/CerealsAir12240.3234
1723-Jul-2012On SiteAnne DodsworthBeveragesSea28281.4204
1813-Jan-2014On SiteMargaret PeacockBeveragesRoad28667.8462
1911-Mar-2014On SiteRobert KingDairy ProductsRoad502440.5234
2016-Dec-2012On SiteJanet LeverlingDairy ProductsSea15657.3696
2122-Apr-2014On SiteJanet LeverlingBeveragesSea30574.8108
Sheet1


I get the result below with the code if I comment out the Delete and Selection.Autofilter lines (note the date in AU1)
Book1.xlsb
ABCDEFGHIATAU
1Order DateSiteSP NameCategory NameShip ViaQuantityOrder Total24-Aug-2023
1523-Aug-2023On SiteAnne DodsworthCondimentsAir301456.6692
Sheet1


@vmjan02 try
VBA Code:
Sub AutoData()

   
    Dim lRow2 As Long
    Dim lColumn2 As Long
   
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False

    lRow2 = Range("A" & Rows.Count).End(xlUp).Row
    lColumn2 = Range("A1").End(xlToRight).Column
        
    ' Range(Cells(1, "A"), Cells(lRow2, lColumn2)).Select 'this line isn't really needed, just to demo the syntax
   
   
    With ActiveSheet.Range("A1:A" & lRow2)
        .AutoFilter Field:=1, Criteria1:=Format(CLng(Range("AU1").Value), "dd-mmm-yyyy")
      
        On Error Resume Next
        .Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12).EntireRow.Delete
        On Error GoTo 0
        .AutoFilter
    End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,090
Latest member
fragment

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