Truncate Whole Words within Max Length Set

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm looking for a way to truncate the length of data that I'm pulling from another Sheet using XLOOKUP.
Cell Formulas
RangeFormula
B2:B6B2=XLOOKUP([@Quiz],tbl_Books[Quiz],tbl_Books[Title],"")
D2:D6D2=LEN([@Title])

I tested out using the LEN Function to limit the number of characters to 48.

TEST using Left Function (Yellow table)

Excel Formula:
=LEFT(tbl_PrintLog[@Title],48)

It works OK, but I found the partial words distracting, so looked for a solution online to show whole words only.

Cell Formulas
RangeFormula
F2:F6F2=LEFT(tbl_PrintLog[@Title],48)
H2:H6H2=LEN(F2)

TRIM + TEXTBEFORE + LEFT (Blue table)

Excel Formula:
=TRIM(TEXTBEFORE(LEFT(tbl_PrintLog[@Title],48)," ",-1))

Unfortunately, this method is truncating too many words even though it's set to 48 max.

Cell Formulas
RangeFormula
J2:J6J2=TRIM(TEXTBEFORE(LEFT(tbl_PrintLog[@Title],48)," ",-1))
L2:L6L2=LEN(J2)

I was thinking perhaps I could count the number of words first, then somehow only grab the words that fall within a maximum length of 48 characters… and disregard any whole words that land on 49 and up.

The following formula counts the number of words (characters) separated by a single space.

Excel Formula:
=TRIM(TEXTBEFORE(LEFT(tbl_PrintLog[@Title],48)," ",-1))

The following GREEN Table are the desired results:

VBA Testing.xlsm
NOP
1Desired OutputDataLEN
2A Funny Thing Happened on the Way to School...xxxxxxxxx46
3A Pirate's Mother Goose (And Other Rhymes)xxxxxxxxx42
4Alexander Hamilton: From Orphan to Foundingxxxxxxxxx43
5Back-to-School Fright From the Black Lagoonxxxxxxxxx43
6Every Planet Has a Place: A Book About Our Solarxxxxxxxxx48
Truncate
Cell Formulas
RangeFormula
P2:P6P2=LEN(N2)

Any assistance with this would be greatly appreciated.


Thanks…
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Is this any use?

23 05 29.xlsm
B
2A Funny Thing Happened on the Way to School...
3A Pirate's Mother Goose (And Other Rhymes)
4Alexander Hamilton: From Orphan to Founding Father
5Back-to-School Fright From the Black Lagoon
6Every Planet Has a Place: A Book About Our Solar System
7
8A Funny Thing Happened on the Way to School...
9A Pirate's Mother Goose (And Other Rhymes)
10Alexander Hamilton: From Orphan to Founding
11Back-to-School Fright From the Black Lagoon
12Every Planet Has a Place: A Book About Our Solar
Truncate
Cell Formulas
RangeFormula
B8:B12B8=LET(s,LEFT(B2,49),IF(LEN(s)<49,s,TEXTBEFORE(s," ",-1)))
 
Upvote 1
Solution
Is this any use?
Hi Peter,

I'd say it's extremely useful. (y)

To incorporate it with my xlookup's return value, I simply replaced the cell reference (B2) in your code with my xlookup formula; it worked like a charm.


Cell Formulas
RangeFormula
Z2:Z6Z2=LET(s,LEFT(XLOOKUP(tbl_PrintLog[@Quiz],tbl_Books[Quiz],tbl_Books[Title],""),49),IF(LEN(s)<49,s,TEXTBEFORE(s," ",-1)))

This will even save me a little bit on printer ink and leave the necessary room I need to make my notations at the end of the book Title.

Thank you again for repeatedly sharing your expertise with me. It's very much appreciated.
 
Upvote 0
You're welcome. Glad it worked for you. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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