How to find and truncate text after the first space before the 38th character

Seric

New Member
Joined
Feb 21, 2019
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am trying to write either a formula or Macro to find the last space in a string before the 38th character. My problem is that my data is not standard and I don't want to truncate in the middle of a word that falls at 37 characters. See example below.

Free-bird Real Opportunities And Encyclopedias LLP = 50 Char

Free-bird Real Opportunities And Ency = 37 char truncation

The goal would look like the truncation below where Encyclopedias is not cut in the middle.

Goal = Free-bird Real Opportunities And

Subsequently I will need to pull out the remaining portion of the name (Encyclopedias LLP) into a different cell.


<tbody>
</tbody>


<tbody>
</tbody>
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi & welcome to MrExcel.
How about
Code:
Sub Seric()
   Dim a As Long
   Dim V As String
   V = Range("J2").Value
   a = InStrRev(V, " ", 38)
   Range("K2") = Left(V, a - 1)
   Range("L2") = Right(V, Len(V) - a)
End Sub
Change ranges to suit
 
Upvote 0
Or with formulas:

A
1Free-bird Real Opportunities And Encyclopedias LLP
2
3Free-bird Real Opportunities And
4Encyclopedias LLP

<tbody>
</tbody>
Sheet12


Worksheet Formulas
CellFormula
A4=TRIM(MID(A1,LEN(A3)+1,LEN(A1)))

<tbody>
</tbody>

<tbody>
</tbody>

Array Formulas
CellFormula
A3{=IF(LEN(A1)<38,A1,LEFT(A1,MAX(IF(MID(A1,ROW(INDIRECT("2:38")),1)=" ",ROW(INDIRECT("1:37"))))))}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>
 
Last edited:
Upvote 0
This is FABULOUS, thank you!

I modified my code to find the values in the entire column that I need to analyse, but the debugger is getting caught by the cell's who's length is less that 38 already. Suggestions?

Sub Seric()
Dim a As Long
Dim V As String

For i = 2 To 161

V = Cells(i, "B").Value
a = InStrRev(V, " ", 38)

Cells(i, "C") = Left(V, a - 1)
Cells(i, "D") = Right(V, Len(V) - a)

Next i
End Sub
 
Upvote 0
How about
Code:
Sub Seric()
   Dim a As Long, i As Long
   Dim V As String
   
   For i = 2 To 161
      If Len(Cells(i, "B")) < 39 Then
         Cells(i, "C") = Cells(i, "B")
      Else
         V = Cells(i, "B").Value
         a = InStrRev(V, " ", 38)
         
         Cells(i, "C") = Left(V, a - 1)
         Cells(i, "D") = Right(V, Len(V) - a)
      End If
   Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top