Filter Dates Stored In An Array

fiberboysa

Board Regular
Joined
Apr 25, 2012
Messages
106
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a simple worksheet which has many dates in an column and I want to filter only Sundays from them. This is a very easy task if you can add a column in data but you cant. So as far as I know it can be done if you store all these dates in an Array in VBA and then filter that column with this Array.
I got a few codes from different website and make them to store all Sundays in that column in an Array but I can't apply filter to that column with this array. So its requested to please suggest a way to do the job. Following is my code for your reference...

VBA Code:
Sub Filter_Sundays()
Dim LR As Integer
Dim Sundays() As Variant
ReDim Sundays(0)
    LR = Workbooks("Filter Sundays").Sheets("Sheet1").Cells(Workbooks("Filter Sundays").Sheets("Sheet1").Rows.Count, "V").End(xlUp).Row
    For x = 6 To LR
        If Weekday(Range("V" & x)) = 1 Then
            ReDim Preserve Sundays(0 To UBound(Sundays) + 1) As Variant
            Sundays(UBound(Sundays)) = Range("v" & x)
        End If
    Next
    ActiveSheet.Range("$V$5:" & LR).AutoFilter Field:=1, Operator:= _
        xlFilterValues, Criteria1:=Array(Application.Transpose(Sundays))
End Sub

Thanks in advance!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try the following changes :

Instead of:
VBA Code:
Sundays(UBound(Sundays)) = Range("v" & x)
Change to:
VBA Code:
Sundays(UBound(Sundays)) = Format(Range("v" & x), "mm-dd-yyyy")
Amend "mm-dd-yyyy" to the actual format used in column V

Instead of :
VBA Code:
 ActiveSheet.Range("$V$5:" & LR).AutoFilter Field:=1, Operator:= _
        xlFilterValues, Criteria1:=Array(Application.Transpose(Sundays))
Change to:
VBA Code:
ActiveSheet.Range("$V$5:$V" & LR).AutoFilter Field:=1, Criteria1:=Sundays, _
    Operator:=xlFilterValues
 
Upvote 0
Solution
Try the following changes :

Instead of:
VBA Code:
Sundays(UBound(Sundays)) = Range("v" & x)
Change to:
VBA Code:
Sundays(UBound(Sundays)) = Format(Range("v" & x), "mm-dd-yyyy")
Amend "mm-dd-yyyy" to the actual format used in column V

Instead of :
VBA Code:
 ActiveSheet.Range("$V$5:" & LR).AutoFilter Field:=1, Operator:= _
        xlFilterValues, Criteria1:=Array(Application.Transpose(Sundays))
Change to:
VBA Code:
ActiveSheet.Range("$V$5:$V" & LR).AutoFilter Field:=1, Criteria1:=Sundays, _
    Operator:=xlFilterValues
Worked perfect... I clanged the code to following and it worked. Thank you!
VBA Code:
Sub Filter_Sundays()
Dim LR As Integer
Dim Sundays() As Variant
ReDim Sundays(0)
    LR = Workbooks("Filter Sundays").Sheets("Sheet1").Cells(Workbooks("Filter Sundays").Sheets("Sheet1").Rows.Count, "V").End(xlUp).Row
    For x = 6 To LR
        If Weekday(Range("V" & x)) = 1 Then
            ReDim Preserve Sundays(0 To UBound(Sundays) + 1) As Variant
            Sundays(UBound(Sundays)) = Format(Range("v" & x), "[$-x-sysdate]dddd, mmmm dd, yyyy")
        End If
    Next
    ActiveSheet.Range("$V$5:$V" & LR).AutoFilter Field:=1, Criteria1:=Sundays, _
    Operator:=xlFilterValues
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

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