Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Handling different date format in excel

We have an Add-in which accepts date as the input to one of the UDF. User can input the date in different format. The problem that we have is when a user inputs a date excel automatically parses it into system define date format. For ex. if an user enters October 08, 2018 as 08/10/2018(dd/MM/yyyy) then excel transforms it into 10/08/2018. This creates problem while creating the UDF. We thought of pluging some event handler as soon as user enters and value, and then if that value is a date then we'll solve this issue by considering date as string literal so that excel will not identify it as date and keep as it is. But in event handler(sheet changed) itself we get the converted value. Any clue how we can handle this kind of situation.

private void adxExcelAppEvents_SheetChanged(object sender, object sheet, object range)
    {
        Excel.Range xlRange = null;
        Excel.Worksheet worksheetPerformingSelection = null;

        try
        {
            xlRange = range as Excel.Range;
            worksheetPerformingSelection = sheet as Excel.Worksheet;
            // check if this can not transform the date to current system's setting format.
            if (ExcelApp.WorksheetFunction.IsNumber(range))
            {
                string val = xlRange.Value.ToString();
                DateTime dt = new DateTime();
                // check for date 
                if (CustomDateTime.TryParse(val, out dt))
                {
                    // if it's a date then store it as string literal.
                    //  xlRange.Value = "'" + dt.ToString();
                }
            }
        }
        finally
        {

        }
    }

Comments