Share via

How do I identify the character count of specific cells - P2

Anonymous
2023-04-25T17:45:27+00:00

Hello All,

Earlier this month I posted a similar question to this one and was provided with a formula that formatted the cells that held a specific character count or greater.

I now need to tweak that a little bit - I would like to format cells that fall between two specific character lengths. I don't know how to tweak the formula that I was provided in my previous post to accomplish this.

For example - I want to locate all of the cells that contain 9 characters or less, but when I plug in =LEN(A1)<=9, all of the empty cells are formatted as well - which is the majority of the selected area on my spreadsheet. And of course, I don't want that. :)

Could someone please provide me with a formula that would work for me here?

Much Thanks!

WB

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2023-04-25T18:43:41+00:00

    Hi WaynePB!

    To format cells that fall between two specific character lengths, you can use a formula with the "AND" function that checks if the length of the cell's content is greater than or equal to the minimum length and less than or equal to the maximum length. Here's how you can do it:

    Select the cells you want to apply the formatting to. Go to the "Home" tab on the ribbon and click on "Conditional Formatting". Select "New Rule". Choose "Use a formula to determine which cells to format". In the "Format values where this formula is true" box, enter the following formula: =AND(LEN(A1)>=5,LEN(A1)<=10). Replace "A1" with the top-left cell in the selected range, "5" with your minimum character length, and "10" with your maximum character length. Click on the "Format" button and choose the formatting you want to apply to the cells that meet the criteria. For example, you could choose to fill the cells with a different color. Click "OK" to close the "Format Cells" dialog box. Click "OK" to close the "New Formatting Rule" dialog box. The above steps will highlight all cells in the selected range with a character count between 5 and 10. If you want to change the minimum and maximum character lengths, simply replace "5" and "10" with the desired values in the formula.

    Alternatively, you can use VBA code: Sub HighlightCellsByLength() Dim minLen As Long Dim maxLen As Long Dim rng As Range Dim cell As Range

    minLen = 5 ' Replace with your minimum character length maxLen = 10 ' Replace with your maximum character length Set rng = Selection ' Change the range to your desired range

    For Each cell In rng If Len(cell. Value) >= minLen And Len(cell. Value) <= maxLen Then cell. Interior.Color = RGB(255, 255, 0) ' Change the color to your desired color End If Next cell End Sub

    Replace the values for "minLen" and "maxLen" with your desired minimum and maximum character lengths. Change the "Set rng" line to select the range you want to apply the formatting to. Save the module. To run the code, go back to your worksheet and select the range you want to apply the formatting to. Press Alt + F8 to open the Macro dialog box. Select the "HighlightCellsByLength" macro and click "Run". The above VBA code will loop through all cells in the selected range and check if the length of the cell's content is greater than or equal to the minimum length and less than or equal to the maximum length. If the cell meets the criteria, the code will highlight the cell with the specified color.

    Note that this code will only work for one selected range at a time. If you want to apply the formatting to multiple ranges, you'll need to run the macro for each range separately.

    Best Regards, Shakiru

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-04-25T19:17:54+00:00

    To format cells that fall between two specific character lengths, you can use a formula with the "AND" function that checks if the length of the cell's content is greater than or equal to the minimum length and less than or equal to the maximum length.

    Hi Shakira,

    You have Once Again come through for me! I greatly appreciate it.

    I knew it would be a formula that I was vaguely familiar with but just didn't know how to construct it.

    Thanks Again!

    WB

    0 comments No comments