Fixing an office script to filter a list populated by xlookup formula

R. Kent 0 Reputation points
2024-07-31T12:36:10.15+00:00

I'm writing an Office Script to click a button and further filter a list (which is already filtered via xlookup). I want the script to show only the employees who's training is out of date.

I receive the "AutoFilter apply: The argument is invalid or missing or has an incorrect format" output message when I run the following script. It does not produce a "Problem" output.

Is anyone able to spot where I'm messing this up?

function main(workbook: ExcelScript.Workbook) {

    let selectedCell = workbook.getActiveCell(); //may not need this line

    let selectedSheet = workbook.getActiveWorksheet();

    let range = selectedSheet.getRange("A13:C160"); //column headers in row 13: Employee Email, CPR Status, Next Enrolled Date

    let autoFilter = selectedSheet.getAutoFilter();

//want to clear any prior filter before the new filter gets applied

    if (autoFilter) {selectedSheet.getAutoFilter().clearCriteria();}

    if (!autoFilter) {autoFilter = selectedSheet.getAutoFilter();}

   

    let today = new Date("I2"); //don't think I need this line

    let expireDate = new Date("J2");

    //let formattedDate = expireDate.toLocaleDateString("en-US"); I don't know if this is necessary or not

//I want the filter to return employees who either have no complete date in the "CPR Status" column AND the people whose training date is expire.

    const filterCriteria: ExcelScript.FilterCriteria = {

      filterOn: ExcelScript.FilterOn.custom,

      criterion1: '<==$expireDate', //the script runs with "" and '' - I don't know what's better

      criterion2: "",

      operator: ExcelScript.FilterOperator.or

    }

    autoFilter.apply(range, 1, filterCriteria)

}

The old VBA code that worked in the desktop version of excel looked like this:

Private Sub FilterCPR_Click()

Dim sDate As Date '(don't actually need this)

Dim eDate As Date

sDate = Range("J2").Value

eDate = Range("I2").Value '(don't actually need this either)

ActiveSheet.Range("B13:C160").AutoFilter 1, "<=" & sDate, xlOr, ""

End Sub

Thank you for any help!!

CPR Help.jpg

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,914 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,715 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.