Written by Allen Wyatt (last updated March 28, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Jacob wants to format a cell so that when he enters a value, if it is above a predetermined threshold, it will show the threshold. For example, if the cell is formatted so that 50 is the highest number he can enter, should he enter 60, the cell will display 50.
If all you want to do is to modify what is displayed in the cell, you could create a simple custom format in this manner:
Figure 1. The Number tab of the Format Cells dialog box.
This approach doesn't modify what is stored in the cell; it simply changes what is displayed in the cell—the largest value it displays is 50, and it will display it in red. The fact that the over-threshold value is still stored in the cell may cause other problems for your worksheet, which may be more trouble than it is worth.
Jacob's situation is typically handled through the use of the Data Validation capabilities of Excel. You would simply set up a rule that indicates what to do if the value in the cell is outside of whatever bounds you want:
Figure 2. The Settings tab of the Data Validation dialog box.
This ensures that someone can only enter a value between 0 and 50 in the cell. If they try to enter a value outside of this range, then whatever error message you specified (step 7) is displayed and the user has the chance to enter a different value.
This approach may not fulfill Jacob's desires, however, as he would simply like to have the value in the cell be changed to the threshold value. In this case, you would need to resort to using a macro. Here's a simple one that could be used:
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("B7")) Is Nothing Then If Target > 50 Then Target = 50 End If End Sub
This macro is implemented as an event handler. To use it, right-click on the worksheet's tab and choose Code from the resulting Context Menu. The above macro then can be pasted into that code window. You will also need to modify which cell it is that you want the macro to pay attention to; as written, it only kicks into action if you try to enter the value into cell B7.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13638) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Do you need to use symbols frequently in your Excel data? The common way to insert them is by using the Symbol dialog ...
Discover MoreWhen you filter rows in your data, you may want to later number those rows. This tip provides a variety of ways you can ...
Discover MoreThe easy way to get rid of spaces at the beginning or end of a cell's contents is to use the TRIM function. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-03-28 09:00:54
Dave Bonin
I think I'd just include a cell (to the right of the data entry cell) with a formula that potentially modifies the user entry to a value within the acceptable range of values.
If the acceptable range were 10 to 50, I might use a formula like:
= MAX( MIN( A1, 50 ), 10 )
Keep it simple.
2019-06-06 20:56:39
StevenM
Thanks for both links. That helps.
2019-06-05 10:38:56
Allen
StevenM:
Any of the tips in this category (Custom Formats) would be helpful to you:
https://excelribbon.tips.net/C0752_Custom_Formats.html
-Allen
2019-06-05 10:37:04
J. Woolley
@StevenM
https://exceljet.net/custom-number-formats
2019-06-04 12:39:41
StevenM
Can you provide a link to a site that discusses options and syntax for creating custom number formats? I can't find through Microsoft any guidance on what is or isn't allowed in a custom format, or the rules governing proper syntax. I often see creative solutions from you, such as in this answer, but can't find rules or guidance.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments