Function to truncate string

Xx7

Board Regular
Joined
Jan 29, 2011
Messages
126
In cell B7 I have the letters "abcdefghijkl". I would like a function called "truncate" that takes characters off both the left and right sides.

=truncate(B7, # of chars to delete from left, # of chars to delete from right)

So....

=truncate(B7, 2, 4)


would return "cdefgh"
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
Code:
Function truncate(c As Range, l As Integer, r As Integer)

truncate = Mid(c.Value, l + 1, Len(c.Value) - l - r)

End Function

Or you could make use of the following existing functions...
Excel Workbook
ABCD
1abcdefghijklcdefgh24
Sheet2
 
Upvote 0

Xx7

Board Regular
Joined
Jan 29, 2011
Messages
126
hmmm.... doesn't seem to work for me on XL2010. I've never used functions before. Any tricks to it? I assume I just throw the code in there like a macro and can use it like the original functions like "sum", "average".


I put:

=truncate(B7, 2,4) and I got an error
 
Upvote 0

Forum statistics

Threads
1,190,666
Messages
5,982,169
Members
439,763
Latest member
leighsha101

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
Top