How to Split or Overwrite the First 27 Characters of a Cell Using VBA
Question details
The user wants to use VBA to overwrite the active cell by retaining only the first 27 characters, while preserving the remaining text by moving it to an adjacent cell.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating text manipulation to split long strings within a cell based on an exact character count limit.
- Observed behavior
- The user requires a macro that accurately isolates the first 27 characters using string functions and shifts the subsequent characters to the next cell.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled the Developer tab in your spreadsheet application to access the VBA editor.
Use Left and Mid VBA Functions to Split Cell Text
Utilize VBA's built-in string functions to extract the exact character count and offset the remaining text to a cell directly below.
The Left function extracts a specified number of characters from the start of a string, while the Mid function extracts characters starting from a designated position. By combining these, you can seamlessly split cell contents.
Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.
In the top menu, click on 'Insert' and select 'Module' to create a blank workspace for your macro.
Copy and paste the following code into the module window: Sub CopyTwentySevenToNextCell() Dim first27 As String Dim after27 As String With ActiveCell first27 = Left(.Value, 27) after27 = Mid(.Value, 28) .Value = first27 .Offset(1, 0).Value = after27 End With End Sub
Close the VBA editor. Select the cell containing your long text, press Alt + F8, select 'CopyTwentySevenToNextCell', and click 'Run'.

Execute Macros Seamlessly with WPS Spreadsheet
WPS Spreadsheet provides robust support for VBA macros in its advanced versions, allowing you to automate complex text manipulation tasks effortlessly while remaining fully compatible with standard spreadsheet formats.
- 1. Install WPS Office: Download and install the latest version of WPS Office.
- 2. Open Your Workbook: Open your Macro-Enabled Workbook (.xlsm) directly in WPS Spreadsheet.
- 3. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on the 'VBA Editor' icon.
- 4. Apply the Macro: Insert your text-splitting macro code and run it directly within the application to automate your workflow.

Frequently Asked Questions
Can I split the text into the next column instead of the next row?
Yes. In the provided VBA code, modify the '.Offset(1, 0)' portion to '.Offset(0, 1)'. This adjustment tells the macro to move zero rows down and one column to the right.
What happens if the cell contains fewer than 27 characters?
The macro will not cause an error. The 'Left' function will simply extract all the available characters, and the 'Mid' function will return an empty string for the destination cell.
How do I apply this macro to multiple cells at once?
You can modify the macro to loop through a selected range. Instead of just referencing 'ActiveCell', wrap the logic in a 'For Each cell In Selection' loop, replacing '.Value' with 'cell.Value' and handling the offsets accordingly.




