Share via

How to have a Conditional Formatting with variable digits

DeanH 20 Reputation points
2026-02-25T14:02:36.5466667+00:00

I wish to create a conditional formatting rule to highlight any cell that has four digits together - like a Year but not a date format;
so to highlight the cell with "2026 Winter Olympics" where the year could be any year (not just 2026).

Any help will be appreciated
Thanks

Microsoft 365 and Office | Excel | For home | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. Ashish Mathur 101.7K Reputation points Volunteer Moderator
    2026-03-01T23:26:41.75+00:00

    Hi,

    This conditional formatting formula works

    =NOT(ISERROR(REGEXEXTRACT(A2,"\d{4}")))

    Hope this helps.

    User's image


3 additional answers

Sort by: Most helpful
  1. Dana D 0 Reputation points
    2026-03-07T03:32:31.7366667+00:00

    any cell that has four digits together - like a Year

    For the poster, you have an excellent solution, but functions like NOT, and ISERROR would not be necessary if looking for 4 digit numbers as in column C, unlike that of column A.

    User's image

    0 comments No comments

  2. Katerina-N 7,000 Reputation points Microsoft External Staff Moderator
    2026-02-25T19:48:57.2+00:00

    Hello DeanH,

    Thank you for posting your question in the Microsoft Q&A forum!

    I understand that you are having issue with conditional formatting in Excel. I truly understand how you feel. Let me assist you go through this situation.

    With your situation, I suggest you try this:

    1. Try 1: Highlights any cell that contains 4 digits anywhere in the text
    • Select the cells you want to format
    • Go to Home > Conditional Formatting > New Rule
    • Choose Use a formula to determine which cells to format
    • Enter this formula: =REGEXMATCH(A1,"\d{4}")
    • Click Format…, choose your style
    • Click OK

    Works for: 2026 Winter Olympics. No helper cells

    1. Try 2:
    • Select the cells you want to format
    • Go to Home > Conditional Formatting > New Rule
    • Choose Use a formula
    • Enter this formula: =SUMPRODUCT(--ISNUMBER(--MID(A1,ROW($1:$99),4)))>0
    • Click Format…
    • Click OK

    Finds any 4‑digit number inside text. Compatible with older Excel

    Important:

    • A1 must be the top‑left cell of the selected range
    • Excel does NOT support regex in SEARCH, which is why your original formula failed

    I hope this will help with your situation. Please feel free to reach back if you have further update or more questions.

    Best Regards,


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment”.   

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Q&A Assist
    2026-02-25T14:03:19.2+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To create a conditional formatting rule in Excel that highlights any cell containing a four-digit number (like a year), you can use a formula-based approach. Here’s how you can set it up:

    1. Select the range of cells you want to apply the formatting to.
    2. Go to the Home tab, click on Conditional Formatting, and select New Rule.
    3. Choose Use a formula to determine which cells to format.
    4. In the formula box, enter the following formula:
         =AND(ISNUMBER(VALUE(MID(A1,SEARCH("[0-9][0-9][0-9][0-9]",A1),4))), LEN(A1) > 4)
      
      Replace A1 with the first cell in your selected range.
    5. Click on the Format button to choose the formatting options (like fill color or font style) you want to apply when the condition is met.
    6. Click OK to apply the rule.

    This formula checks for a four-digit sequence within the text and highlights the cell if it finds one. The SEARCH function looks for a four-digit number, and ISNUMBER checks if the result is a number.


    References:


Your answer

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