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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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
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,214,553
Messages
6,120,184
Members
448,949
Latest member
keycalinc

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