Question

I am trying to use a custom format to restrict the amount of text that is displayed in a cell.

For instance, the following custom format will hide the entire cell's contents...

";;;"

But i want to limit the amount of text that is displayed, like only show the first 8 characters.

I have tried various variations of @, but nothing has worked so far.

Was it helpful?

Solution

I don't believe there is practical way of doing this using custom number format. Here is an impractical way: It is impractical because it requires a separate format for each entry and, eventually, you could run out of available number formats. Any difference in format -- whether it is the numberformat, or other things such as font, colors, borders, etc counts as a different format. In Excel 2003 and earlier, there were 4,000 different formats allowed. In 2007+, the number increased to 64,000. In any event, the following Event code will format anything you enter in column A to show only the first 8 characters of text displayed, but will have all of the characters in the cell. Enter the code by right-clicking on the worksheet tab, select View Code, and entering it into the window that opens.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim R As Range
    Dim C As Range
Set R = Range("a:a")
If Not Intersect(Target, R) Is Nothing Then
    Application.EnableEvents = False
    For Each C In Intersect(Target, R)
        C.NumberFormat = ";;;""" & Left(C, 8) & """"
    Next C
    Application.EnableEvents = True
End If

End Sub

Another solution would be to use a separate display column containing only the first 8 characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top