Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Function/Macro that keeps first X characters in a cell and delets rest


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/sh...d.php?t=167722

Microsoft Office Help

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Function/Macro that keeps first X characters in a cell and delets rest

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" wrote in message
...

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/sh...d.php?t=167722

Microsoft Office Help


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Function/Macro that keeps first X characters in a cell and delets

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/sh...d.php?t=167722

Microsoft Office Help

.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Function/Macro that keeps first X characters in a cell and delets rest


thanks , it works


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

Microsoft Office Help

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get rest of characters after last certain character? Wille Excel Programming 3 December 14th 09 12:21 PM
Macro/function that keeps in a cell a given number of characters andrei[_6_] Excel Programming 1 October 27th 09 05:41 PM
Macro analyses Cell . If text found , delets entire row andrei Excel Programming 6 October 1st 09 05:56 PM
macro which delets cell if the cell above has the same keyword andrei Excel Programming 2 September 29th 09 07:46 AM
finding the first blank cell and then executing rest of macro Ed[_25_] Excel Programming 1 December 13th 04 07:24 PM


All times are GMT +1. The time now is 11:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"