Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function/Macro that keeps first X characters in a cell and delets rest

63 views
Skip to first unread message

andrei

unread,
Jan 7, 2010, 3:28:38 AM1/7/10
to

So , i need a macro/function that keeps the first X characters in a cell
( the number is given ) and deletes the remaining characters

Example

In a cell i have the following text : *Mother and father*
I want the macro to keep the first 8 characters . So , in that cell ,
the result should be : *Mother a* . When i mean charcaters , i take into
consideration letter , space or number


--
andrei
------------------------------------------------------------------------
andrei's Profile: 1056
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=167722

[url="http://www.thecodecage.com"]Microsoft Office Help[/url]

Rick Rothstein

unread,
Jan 7, 2010, 3:57:29 AM1/7/10
to
Let's assume your cell is A1; this line of code should accomplish what you
want...

Range("A1").Value = Left(Range("A1").Value, 8)

Since this changes the value in the cell itself, you cannot use a UDF (User
Defined Function), so I'm guessing you will want to use it in a macro. Your
description (as to how you wanted your code to perform) was not detailed
enough to decide how to structure the macro for you.

--
Rick (MVP - Excel)


"andrei" <andrei...@thecodecage.com> wrote in message
news:andrei...@thecodecage.com...

J_Knowles

unread,
Jan 7, 2010, 8:46:01 PM1/7/10
to
Try this out:

Sub leftXChrs()
'keeps left x characters of a selected range
Dim c As Range
Dim mychrs As Long
On Error Resume Next
mychrs = InputBox("Be sure to select a range of cells. " _
& Chr(13) & "How many characters to keep?", 1)
If mychrs = False Or Not Application.IsNumber(mychrs) Then
Exit Sub
End If
For Each c In Selection
c.Value = Left(c.Value, mychrs)
Next c
End Sub

HTH,
--
Data Hog


"andrei" wrote:

>
> So , i need a macro/function that keeps the first X characters in a cell
> ( the number is given ) and deletes the remaining characters
>
> Example
>
> In a cell i have the following text : *Mother and father*
> I want the macro to keep the first 8 characters . So , in that cell ,
> the result should be : *Mother a* . When i mean charcaters , i take into
> consideration letter , space or number
>
>
> --
> andrei
> ------------------------------------------------------------------------
> andrei's Profile: 1056
> View this thread: http://www.thecodecage.com/forumz/showthread.php?t=167722
>

> [url="http://www.thecodecage.com"]Microsoft Office Help[/url]
>
> .
>

andrei

unread,
Jan 12, 2010, 5:33:08 AM1/12/10
to

thanks , it works


--
andrei
------------------------------------------------------------------------
andrei's Profile: 1056
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=167722

[url=&quot;http://www.thecodecage.com&quot;]Microsoft Office Help[/url]

0 new messages