Home » excel » Truncate a string inside an Excel cell if the char count is greater than than a value

Truncate a string inside an Excel cell if the char count is greater than than a value

Posted by: admin May 14, 2020 Leave a comment

Questions:

In Excel, for each row of the sheet I have various length strings (a1,a2,a3…). In cell B2 I have =Length(A1) to count the chars inside the string.

I need a formula/function that can truncate all strings in column A which have a character count > 20
Something like:

$string = THIS IS A LONG STRING I WANT TO TRUNCATE IF EXCEEDS 20 CHARS;
if ($string > 20)
{
   COUNT 20 CHARS FROM THE BEGINNING OF STRING AND CUT THE REST
} 
else 
{
 skip 
}
How to&Answers:

You can just read the 1st 20 characters, it doesn’t matter if there are fewer;

 =left(A1, 20)

Answer:

Use this,

=LEFT(DataCell,20)

eg:

=LEFT(A1,20)

Answer:

Use the Len function for Length and Left function to use only the first 20 caracters:

Dim myString As String

If (Len(myString) > 20) Then
    myString = Left(myString, 20)
End If