Excel

Split text between cells in a column as you go.

Ease of Use

Easy

Version tested with

2000, 2003 

Submitted by:

mdmackillop

Description:

If the text entered in a cell will exceed the length of that cell, this procedure will split the text into the cells below when the Enter key is pressed. 

Discussion:

Sometimes wrapping text and increasing cell heights is not permissible in a spreadsheet application. Where long text is to be entered, this code will split the text at the nearest space and move the remainder to the cells below; effectively "wrapping" the text within a column. The Target in the Worksheet Module may be coded to restrict functionality to selected ranges if required. Due to proportional spacing, the measures are not exact and some adjustment of a coded ratio may be required to suit the font style and size selected. 

Code:

instructions for use

			

'******************************************** 'In the worksheet module Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub If Len(Target) / Target.Width > Ratio Then SplitText End Sub '******************************************** '******************************************** 'In the standard module Option Explicit 'Default ratio for splitting; adjust to suit font size Public Const Ratio = 0.23 'Suits Arial 10 Sub SplitText() Dim MyText As String Dim WrapLength As Long, StrLen As Long, j As Long Dim SplitRatio As Double Dim NextCell As Range 'SplitRatio = 0.22 'Set own value in place of InputBox if desired SplitRatio = InputBox("Enter ratio", "Cell width/Characters", Ratio) Application.EnableEvents = False 'Return to previous cell If Application.MoveAfterReturnDirection = xlToRight Then Set NextCell = ActiveCell ActiveCell.Offset(0, -1).Select Else ActiveCell.Offset(-1).Select End If WrapLength = Int(ActiveCell.Width) * SplitRatio 'Analyse text for space preceding cell width and split text Do MyText = ActiveCell.Text StrLen = Len(MyText) If StrLen > WrapLength Then For j = WrapLength To 0 Step -1 If j = 0 Then Exit For If Mid(MyText, j, 1) = " " Then ActiveCell.Formula = Left(MyText, j) ActiveCell.Offset(1, 0).Formula = Right(MyText, StrLen - j) Exit For End If Next ActiveCell.Offset(1, 0).Select End If Loop Until Len(ActiveCell) <= WrapLength ActiveCell.Offset(1, 0).Select 'Move to right based on MoveAfterEnter If Not NextCell Is Nothing Then NextCell.Select Application.EnableEvents = True End Sub Sub Retry() Dim Cel Dim MyText As String Application.EnableEvents = False 'Cocatenate cell strings For Each Cel In Selection MyText = MyText & Cel Cel.ClearContents Next 'Move to first cell of selection and write text Selection.Cells(1).Select Selection.Formula = MyText 'Move to next cell If Application.MoveAfterReturnDirection = xlToRight Then ActiveCell.Offset(0, 1).Select Else ActiveCell.Offset(1).Select End If SplitText End Sub '********************************************

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Tools/Project Explorer
  4. Select a Worksheet module and paste in the first bit of code
  5. Insert a standard module and paste in the second bit of code
  6. Now select File/Close and Return To Microsoft Excel
  7. Save the file
 

Test the code:

  1. Enter text into a cell on the spreadsheet with the coded module, which exceeds the width of the cell and press enter. The text should be "wrapped" into the cells below.
  2. The sample file contains some text which will be entered on clicking the button. An inputbox will request a figure to be entered; the default is 0.23; try different figures to suit your font style and size.
  3. Change the Ratio constant figure in the Standard Module to that found to be most suitable.
  4. The Retry function allows you to "reassemble" your text into the first cell and test different ratios.
 

Sample File:

SplitText.zip 12.76KB 

Approved by mdmackillop


This entry has been viewed 354 times.

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