Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:34:33+00:00 2026-06-07T20:34:33+00:00

I am looking to have a validation in an excel sheet which is generated

  • 0

I am looking to have a validation in an excel sheet which is generated using C# such that it allows either date value or the text “train” in the excel cell. I found XlDVType.xlValidateDate for validating date in a cell and XlDVType.xlValidateList to allow text values. But I want a combination of both, user should either be able to enter date or a particular text in the cell. Can this be accomplished using XlDVType.xlValidateCustom ??

any help on this would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:34:36+00:00Added an answer on June 7, 2026 at 8:34 pm

    I want a combination of both, user should either be able to enter date
    or a particular text in the cell.

    The following code snippet sets the desired validation for cell A1 in your worksheet:

    Excel.Range range = xlWorkSheet.Range["A1"];
    range.ClearFormats();
    range.Validation.Delete();
    range.Validation.Add(Excel.XlDVType.xlValidateCustom,
        Formula1: "=OR(EXACT(LEFT(CELL(\"format\",A1)),\"D\"),EXACT(A1,\"train\"))");
    

    The most complicated part is the actual validation formula. It uses the following functions:

    EXACT(cell,string) compares the contents of a cell with a literal string and returns TRUE only if they are exactly the same.

    CELL("format",cell) returns a code containing the format of the indicated cell, where the code for a date always starts with a D. See Office help for CELL for an explanation and a list of all possible codes. This snippet only looks at the first character, which has to be a D in order for the contents to be formatted as some kind of date.

    LEFT(text,count) returns the leftmost count characters in text, with count defaulting to 1. In this case, it returns TRUE if the contents are some kind of date.

    OR(logical1, logical2) returns logical1 OR logical2, so TRUE if the contents are either a date, or exactly equal to the string "train".

    This solution is not yet ideal, because A1 is hardcoded into the formula. There is a way around that using INDIRECT, described here. You might need that, for example if you want to apply the validation to a whole range of cells in one go. In that case, the hardcoded A1 needs to be replaced by INDIRECT("RC",FALSE), which is the mechanism to indicate the current shell. You can read it as a short-cut for INDIRECT("R"&ROW()&"C"&COLUMN(),FALSE).

    With that adjustment, he last line of code then looks like this (tested in Excel, but not from C#):

    range.Validation.Add(Excel.XlDVType.xlValidateCustom,
        Formula1: "=OR(EXACT(LEFT(CELL(\"format\",INDIRECT(\"RC\",FALSE))),\"D\"),EXACT(INDIRECT(\"RC\",FALSE),\"train\"))"
    

    Update

    As the OP points out, there appears to be a problem with this approach if a date has first been entered into the cell, and then is changed to an incorrect string or number value — see the comments below. Using the CELL function therefore does not seem to be the best approach.

    However, if the cell format is explicitly set to the (more flexible) Text format, it is possible to impose the validation with the following lines:

    range.Validation.Delete();
    range.NumberFormat = "Text";
    range.Validation.Add(Excel.XlDVType.xlValidateCustom,
        Formula1: =OR(ISNUMBER(DATEVALUE(INDIRECT(\"RC\",FALSE))),EXACT(INDIRECT(\"RC\",FALSE),\"train\"));
    

    Again tested in Excel, but not from C#.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using a validation framework (something we have cooked up ourselves) which allows
I have set up validation for a HTML form using jQuery and I'm looking
I have been looking at many ASP.Net MVC client side validation ideas including xVal.
I have a text field and am looking to have if be valid for
I'm looking to have it so that if someone enters information in a form,
All, I'm working on an application that allows authorized users to upload Excel spreadsheets
I have been looking for a decent jQuery validation methodology. I found this jsFiddle
I am looking to validate a form element using ajax validation using the OnBlur
I have been looking for a way to make form validation as easy and
I have been looking at either coding from scratch a basic login page +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.