Excel

Display and print cells containing more than 1024 characters

Ease of Use

Intermediate

Version tested with

2000, 2002, 2003 

Submitted by:

byundt

Description:

Stops Excel from truncating text when cell contains many characters. Allows cells containing more than 1024 characters to display and print in entirety. 

Discussion:

Excel truncates "excess" characters from cells containing a lot of text. More precisely, the characters are still there (you can see them in the formula bar)?but Excel won't display them in the cell or allow them to be printed. Excel specifications say that no more than 1024 characters in a cell may be displayed or printed, even though the cell can hold 32767 characters. Vishal Shah observed that you can exceed the 1024 limit by adding line feed characters (ASCII 10, as produced by ALT + Enter) after each line beyond the first 1024 characters. Here is a macro that automates the process of adding the required line feed characters to each cell in a selection. 

Code:

instructions for use

			

Option Explicit Sub DisplayLongText() 'Adds line feed characters as required on cells in selection that are longer than 1024 characters Dim cel As Range Dim col As Long For Each cel In Selection AddLineFeeds cel, col Next col = 0 'Force line length dialog to display the next time sub runs End Sub Sub AddLineFeeds(cel As Range, col As Long) 'Adds line feed characters at end of each line of text. Some experimentation may be required to set number 'of characters at the wrapping point. 'Code requires Excel 2000 or later because of Replace and InStrRev functions Static lineIncr As Long Dim i As Long, j As Long, pos As Long, StartPos As Long Dim sLeft As String, str As String, sRight As String, sLineFeed As String StartPos = 1 'Value should be between 1 and 1022. The first 1022 characters (plus ASCII 160 & line feed) 'will always fit correctly. The macro will add ASCII 160 space plus line feed characters 'to each line after character StartPos. You may notice that the beginning section of text 'uses a different "break" point than the end. If this bothers you, set StartPos=1; 'it will be reset to the chosen line length later in the sub. With cel If Len(.Value) <= StartPos Then Exit Sub 'Remove line feed characters which may have been added previously. These always follow an ASCII 160 space. sLineFeed = Chr(160) & Chr(10) 'Code puts an ASCII 160 space before every added line feed character str = Replace(.Value, sLineFeed, " ") 'The maximum permitted number of characters on a line. User-specified up to a limit of 256 characters/line If .Column <> col Then 'Use same value as last time if still working in same column lineIncr = Application.Min(InputBox(Prompt:="Please specify the desired column width (in characters)", _ Title:="Long Text In Cell Utility", Default:=.ColumnWidth - 1), 256) col = .Column If StartPos < lineIncr Then StartPos = lineIncr End If sLeft = Left(str, StartPos) 'Excel has no problem wrapping the first 1024 characters pos = InStrRev(sLeft, " ") 'Find right-most space in first 1022 characters If pos = 0 Then 'No space found, so force a break after 1022 characters sLeft = sLeft & sLineFeed sRight = Mid(str, StartPos + 1) Else 'Put ASCII 160 plus line feed characters in place of this right-most space sLeft = Left(str, pos - 1) & sLineFeed sRight = Mid(str, pos + 1) End If pos = 1 'Loop through remainder of text, looking for places to put ASCII 160 plus line feed characters Do j = InStr(pos, sRight, Chr(10)) If j > 0 And j - pos <= lineIncr Then pos = j + 1 Else i = InStrRev(sRight, " ", pos + lineIncr) 'Find right-most space in next lineIncr characters If i > pos Then 'Put ASCII 160 plus line feed characters in place of this right-most space sRight = Left(sRight, i - 1) & sLineFeed & Mid(sRight, i + 1) pos = i + 2 Else 'Didn't find a good place to break the line, so force the break in middle of a word sRight = Left(sRight, pos + lineIncr) & sLineFeed & Mid(sRight, pos + lineIncr + 1) pos = pos + lineIncr + 3 End If End If If Len(sRight) - pos < lineIncr Then Exit Do 'Not enough text left for a full line Loop .Value = sLeft & sRight 'Put the rebuilt string in place of the original End With End Sub

How to use:

  1. Note: Code uses Replace and InStrRev functions, and therefore requires Excel 2000 or later. Will not work on Excel 97!
  2. Select and copy above code (Ctrl + C).
  3. From Excel, open the Visual Basic Editor (VBE) with ALT + F11.
  4. Select any file on left, choose Insert-Module from the menu.
  5. Paste code in right pane (Ctrl + V).
  6. The AddLineFeeds sub variable StartPos defines the character position at which the code begins adding line feed characters. Even though Excel can handle the first 1024 characters without problem, you may want to adjust this variable. The default value of 1 results in text with a more uniform appearance. A high value for StartPos (such as the maximum of 1022) means the macro won't make changes to cells containing only short text.
  7. Close the VBE with ALT + Q.
 

Test the code:

  1. Select one or more cells to process. Cells containing less than StartPos characters will be ignored (they display and print fine as-is).
  2. ALT + F8 to open the macro selector. Choose the DisplayLongText macro, then click the "Run" button
  3. Macro displays an input box for each column being processed. This input box lets you specify the maximum number of characters in a line before a line feed character must be added. The default value is the column width, which is a good starting point.
  4. You may need to change the row height after running the macro. Remember that Excel allows a maximum row height of 409.5 points. If that doesn't provide enough room, you can either merge cells vertically to get more height or increase column width.
  5. If the lines after StartPos characters don't match the width of the preceding lines, then you can rerun the macro and choose a different maximum number of characters/line--or change StartPos to 1.
 

Sample File:

LongTextInCells.zip 12.1KB 

Approved by mdmackillop


This entry has been viewed 190 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express