Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBA question: Condense cell contents to first character

I have a column of first names that I want to condense down to the first
letter only (i.e. Chris becomes C). I have some code that I wrote, but it
doesn't work and I want to process every cell in that column that has a
value. Any help here would be appreciated...

Sub TrimText()
Dim FirstName As String

FirstName = ActiveCell.Text
If Len(FirstName) 1 Then
FirstName = Left(ActiveCell.Text, 1)
ActiveCell.Text = FirstName
End If
End Sub

Also, is it possible to create macros that aren't imbedded in each XLS file?
I'd like to have several files that I can run this macro on.

Sean


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default VBA question: Condense cell contents to first character

Sub TrimText()
Dim FirstName As String
Dim r as Range, cell as Range
set r = ActiveCell.EntireColumn.specialCells(xlConstants,x lTextValues)
for each cell in r
FirstName = cell.Text
If Len(FirstName) 1 Then
FirstName = Left(Cell.Text, 1)
Cell.Text = FirstName
End If
Next cell
End Sub

Record a macro and if you don't have a personal.xls workbook it will create
one if you choose to store the recorded macro in the personal workbook.

Now you can put any common code in your personal.xls workbook. It will be
opened each time excel is started and you will be able to select your macros
from the Tools=Macro=Macros dialog and run them if they are in standard
modules and not marked as private.

--
Regards,
Tom Ogilvy

"Boodahbellie" wrote:

I have a column of first names that I want to condense down to the first
letter only (i.e. Chris becomes C). I have some code that I wrote, but it
doesn't work and I want to process every cell in that column that has a
value. Any help here would be appreciated...

Sub TrimText()
Dim FirstName As String

FirstName = ActiveCell.Text
If Len(FirstName) 1 Then
FirstName = Left(ActiveCell.Text, 1)
ActiveCell.Text = FirstName
End If
End Sub

Also, is it possible to create macros that aren't imbedded in each XLS file?
I'd like to have several files that I can run this macro on.

Sean



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBA question: Condense cell contents to first character

Tom, I ran the macro and get "Runtime error '242': Object required"
What could I be doing wrong? I have the first cell of the target column
selected when I run the macro.

"Tom Ogilvy" wrote in message
...
| Sub TrimText()
| Dim FirstName As String
| Dim r as Range, cell as Range
| set r = ActiveCell.EntireColumn.specialCells(xlConstants,x lTextValues)
| for each cell in r
| FirstName = cell.Text
| If Len(FirstName) 1 Then
| FirstName = Left(Cell.Text, 1)
| Cell.Text = FirstName
| End If
| Next cell
| End Sub
|
| Record a macro and if you don't have a personal.xls workbook it will
create
| one if you choose to store the recorded macro in the personal workbook.
|
| Now you can put any common code in your personal.xls workbook. It will be
| opened each time excel is started and you will be able to select your
macros
| from the Tools=Macro=Macros dialog and run them if they are in standard
| modules and not marked as private.
|
| --
| Regards,
| Tom Ogilvy
|
| "Boodahbellie" wrote:
|
| I have a column of first names that I want to condense down to the first
| letter only (i.e. Chris becomes C). I have some code that I wrote, but
it
| doesn't work and I want to process every cell in that column that has a
| value. Any help here would be appreciated...
|
| Sub TrimText()
| Dim FirstName As String
|
| FirstName = ActiveCell.Text
| If Len(FirstName) 1 Then
| FirstName = Left(ActiveCell.Text, 1)
| ActiveCell.Text = FirstName
| End If
| End Sub
|
| Also, is it possible to create macros that aren't imbedded in each XLS
file?
| I'd like to have several files that I can run this macro on.
|
| Sean
|
|
|


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default VBA question: Condense cell contents to first character

the TEXT property is a read only property. I just replaced the
ActiveCell.Text by deleting "Active". I didn't notice you were assigning the
value to the Text property.

This revision worked for me.

Sub TrimText()
Dim FirstName As String
Dim r As Range, cell As Range
Set r = ActiveCell.EntireColumn.SpecialCells(xlConstants, xlTextValues)
For Each cell In r
FirstName = cell.Text
If Len(FirstName) 1 Then
FirstName = Left(cell.Text, 1)
cell.Value = FirstName
End If
Next cell
End Sub

--
Regards,
Tom Ogilvy


"Boodahbellie" wrote:

Tom, I ran the macro and get "Runtime error '242': Object required"
What could I be doing wrong? I have the first cell of the target column
selected when I run the macro.

"Tom Ogilvy" wrote in message
...
| Sub TrimText()
| Dim FirstName As String
| Dim r as Range, cell as Range
| set r = ActiveCell.EntireColumn.specialCells(xlConstants,x lTextValues)
| for each cell in r
| FirstName = cell.Text
| If Len(FirstName) 1 Then
| FirstName = Left(Cell.Text, 1)
| Cell.Text = FirstName
| End If
| Next cell
| End Sub
|
| Record a macro and if you don't have a personal.xls workbook it will
create
| one if you choose to store the recorded macro in the personal workbook.
|
| Now you can put any common code in your personal.xls workbook. It will be
| opened each time excel is started and you will be able to select your
macros
| from the Tools=Macro=Macros dialog and run them if they are in standard
| modules and not marked as private.
|
| --
| Regards,
| Tom Ogilvy
|
| "Boodahbellie" wrote:
|
| I have a column of first names that I want to condense down to the first
| letter only (i.e. Chris becomes C). I have some code that I wrote, but
it
| doesn't work and I want to process every cell in that column that has a
| value. Any help here would be appreciated...
|
| Sub TrimText()
| Dim FirstName As String
|
| FirstName = ActiveCell.Text
| If Len(FirstName) 1 Then
| FirstName = Left(ActiveCell.Text, 1)
| ActiveCell.Text = FirstName
| End If
| End Sub
|
| Also, is it possible to create macros that aren't imbedded in each XLS
file?
| I'd like to have several files that I can run this macro on.
|
| Sean
|
|
|



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
Insert character at beginning of cell without changing contents elle Excel Discussion (Misc queries) 4 April 3rd 23 06:52 PM
Excel 2007 - Formatting text in cell (character by character) TomC Excel Discussion (Misc queries) 0 January 29th 10 07:25 PM
VBA Question: Move cell contents if... Scott Wagner Excel Programming 6 March 17th 06 10:03 PM
Another cell formatting dependent on cell contents question / message box popup? StargateFan[_3_] Excel Programming 2 January 14th 06 02:47 PM
macro to add alpha character to cell contents Janna[_3_] Excel Programming 2 August 5th 04 08:47 PM


All times are GMT +1. The time now is 10:32 PM.

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

About Us

"It's about Microsoft Excel"