Change number format based on a cell value in Excel

You’ve built an amazing dynamic report; a user selects a business unit to view, and everything updates automatically – it’s beautiful. The only issue is that Company A has sales of $30,000,000, Company B has sales of $30,000.

Displaying values in 1 decimal place millions makes sense for Company A. $30,000,000 becomes $30.0m. But it does not make sense for Company B. $30,000 becomes $0.0m.

The companies have different levels of significance. How can we format numbers so they work with either company?

In this post we look at 2 techniques.

Table of Contents

Download the example file: Click the button below to join the Insiders program and gain access to the example file used for this post.

File name: 0199 Change number format.zip

Watch the video

Change number format based on cell value | Excel Off The Grid

Watch on YouTube

Introduction to number formatting

Number formatting is a text string that tells Excel how to display a number. It doesn’t change the number itself, only how it looks.

We will not go into detail about number formats in this post. If you need to know more check out this post: Excel number formats for accounting & finance you NEED to know

Number formats are separated into 4 sections, all separated by semicolons.

[display of positive values] ; [display of negative values] ; [display of zero values] ; [display of text values]

We can format each section so that positive, negative, zero, and text values all display exactly as we want.

These number formats can include special characters which help to apply the desired formatting.

  • # (hash) represents an optional digit. If a number is required it will display, if not, nothing displays.
  • 0 (zero) represents a forced digit. Even if there is no number, a zero is still displayed. 
  • , (comma) at the end of the number format forces the value to be divided by 1,000. Two commas means it is divided by 1,000,000.
  • , (comma) between # or 0 indicates the thousand separator character,
  • . (period) indicates where to place the decimal point.

Note: Based on regional settings, some characters may be different for you.

For this example, we have two number formats, which change depending on the value.

  • If the sales value is less than 1,000,000, the number format is: #,##0,_);(#,##0,);-,_)
  • If the sales value is 1,000,000, or greater the number format is: #,##0.0,,_);(#,##0.0,,);-,,_)

Now that we have a basic understanding of number formats, let’s examine how to apply them.

Option 1 – Use the TEXT function

The number formatting codes displayed above can be used within the TEXT function.

For this first example, the number formats are contained in a Table called Formats.

Number Formats

To retrieve the format code, we lookup the Threshold using the Sales value.

Get the number format

The formula in cell C4 is:

=INDEX(Format[Format],MATCH(C10,Format[Threshold],1))

This calculation uses an approximate match to return the closest value below the lookup value.

In the screenshot, cell C10 has a value of 30,000,000. The closest value below 30,000,000 is 1,000,000. Therefore the format returned is: #,##0.0,,_);(#,##0.0,,);-,,_)

We can apply format code using the TEXT function.

TEXT has two arguments:

  • Value: the number to format.
  • Format_text: the format to apply.

Using this, we can format any value to appear as we wish.

TEXT format

The formula in cell D10 is:

=TEXT(C10,$C$4)

This formula displays the value in C10 using the number format in cell C4.

The formula has been copied in D12, D14, D16, D18, D20 and D22.

When the values change to Company B, the formats update and apply the new number format.

TEXT format changed

It is important to realize the values are now text which looks like numbers. Therefore, we cannot calculate using these values.

To make this method work in the real world, we would need to:

  • Calculate the actual values in a separate workings area.
  • Display the values using the TEXT function on the face of the report.

Option 2 – Conditional Formatting

The second method uses conditional formatting. It takes a few more steps to apply, but we can continue to use the values as numbers.

Select the numbers in cells E10:E22.

Click Home > Conditional Formatting > New Rule…

Conditional Formatting - New Rule

First, let’s set up the conditional format where the value is greater than or equal to 0.

Click use formula to determine which cells to format.

Enter the following formula:

=ABS($E$10)>=0

ABS calculates the absolute value for E10. Where it is greater than or equal to 0 the conditional formatting applies.

Conditional Formatting Rule applied

Click Format…

In the Format Cells window, select the Number > Custom

In the type box, enter the required number format. In our example, we are using:

#,##0,_);(#,##0,);-_)
Format cells dialog box

Click OK.

Repeat the process for values greater than 1,000,000. The number format applied in the example is:

#,##0.0,,_);(#,##0.0,,);-,,_)

You might be thinking – 30,000,000 is greater than 0 and greater than 1,000,000, so which number format applies?

Click Home > Conditional Formatting > Manage Rules… to view the rules.

Conditional Formatting Window - all rules

The format applied is based on the last format in the list where the formula evaluates to TRUE.

To apply the format based on the first condition, check the Stop If True box for each format.

The numbers now display different formats depending on the value. The values are still numbers, and we can calculate using those numbers.

The drawbacks of this method are:

  • Need to create a separate conditional format for each threshold.
  • If there is a lot of conditional formatting it can have performance issues.

Conclusion

In this post, we’ve seen two ways to apply number formatting based on a cell value. This ensures that we can apply a suitable level of display significance to values regardless of the underlying value.

Related Posts:


Discover how you can automate your work with our Excel courses and tools.

Excel Academy

Excel Academy
The complete program for saving time by automating Excel.

Excel Automation Secrets

Excel Automation Secrets
Discover the 7-step framework for automating Excel.

Office Scripts Course

Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.


Discover how you can automate your work with our Excel courses and tools.

Excel Academy

Excel Academy
The complete program for saving time by automating Excel.

Excel Automation Secrets

Excel Automation Secrets
Discover the 7-step framework for automating Excel.

Office Scripts Course

Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.

4 thoughts on “Change number format based on a cell value in Excel”

    • Hi Peter,

      To give you a 100% correct answer I would need to know quite a bit more about your scenario. For example, should 6779 be formatted as:
      6,779.00 or
      000,006,779.00

      Should a negative (a) have a minus sign (b) be shown in a different color (c) have parentheses around it?

      In its simplest form, which is what I suspect you’re trying to achieve, you should use:
      #,##0.00

      Let me know if that doesn’t give you what you want.

      Reply
  1. If you only have positive numbers, you can create a custom number format like this:
    [>=1000000]0.0,,”M”;[>=1000]0.0,”k”;0.0;@
    which formats numbers like this:
    10.0M
    1.0M
    100.0k
    10.0k
    1.0k
    100.0
    10.0
    1.0

    Reply
    • Hi Jon – Very true, thanks for adding that. I forget about that method.

      Often I feel nervous about guaranteeing that numbers will be positive. Even for sales there are refunds, or “adjustments” which end up being negative values.

      Reply

Leave a Comment