I am trying to filter a set of data (see below) to filter out anything beyond the current date in my automation. I am not sure how to do this at all using the filter activity. Can anybody advise/help me do this? Is it possible?
Do you want to use the filter with excel activities or you can read the excel to a datatable and filter.
Give me a clarification on this…
If you want to filter after converting to a datatable then you can use the LINQ Expressions,
→ Use the Read range workbook activity to read the excel and store in a datatable called dt.
→ Then use the assign activity to write the LINQ Expression,
Below query is for Dates are behind and equal to Today's date
- Assign -> dt = (From row In dt
Where CDate(row("Due date").toString)<=DateTime.Now
Select row
).Copytodatatable()
Below query is for Dates are behind to Todays' date only,
- Assign -> dt = (From row In dt
Where CDate(row("Due date").toString)<DateTime.Now
Select row
).Copytodatatable()
→ Then use the write range workbook activity to write the dt to the excel.
Check the below workflow image for better understanding,
Sub FilterOutFutureDates()
Dim ws As Worksheet
Dim lastRow As Long
Dim today As Date
Set ws = ThisWorkbook.Sheets("Sheet3") ' Change "Sheet1" to your sheet name
today = Date
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Adjust the column as needed
' Clear any existing filters
If ws.AutoFilterMode Then ws.AutoFilterMode = False
' Apply filter to hide rows with dates beyond today
ws.Range("B1:B" & lastRow).AutoFilter Field:=1, Criteria1:="<=" & today, Operator:=xlAnd
End Sub
Sub FilterOutFutureDates()
Dim ws As Worksheet
Dim lastRow As Long
Dim today As Date
Set ws = ThisWorkbook.Sheets("Tasks") ' Change "Sheet1" to your sheet name
today = Date
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Adjust the column as needed
' Clear any existing filters
If ws.AutoFilterMode Then ws.AutoFilterMode = False
' Apply filter to hide rows with dates beyond today
ws.Range("J1:J" & lastRow).AutoFilter Field:=1, Criteria1:="<=" & today, Operator:=xlAnd
End Sub
I updated the code as your sheet name Tasks and Dates Column “J1:J” It takes entire J column and filter it.
I don’t seem to be able to target the workbook. I keep getting the following error:
“Argument ‘Workbook’: BC30512: Option Strict On disallows implicit conversions from ‘String’ to ‘IWorkbookQuickHandle’. The selected value is incompatible with the property type
Activity is valid only inside the “Use Excel File” scope.”
Hi, unfortunately the process doesn’t work for me, there are still future dates in the filtered file when I use this. Is there another way you know of?
dt is the datatable datatype variable variable. You are using the Read range workbook activity above the assign activity right. Create a variable to the output of Read range workbook activity dt. Then your problem will resolve.