Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How do I extract a word from a string?

EG.

I want to only get the first name of the following full names that are
given in column A:

John M. Ford
Albert Cushy
Bertland Sunderland
Hugo Sanchez d'Angelo


My idea is to find how many spaces there is until the first " "
character and then use the mid function until this character (" ") to
create a new string with only the first name in it.

Anyone know how create a synthax like this?

Gabe.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default How do I extract a word from a string?

FirstName = Left(FullName, InStr(FullName, " ") - 1)


"Gabe Tiger" wrote:

EG.

I want to only get the first name of the following full names that are
given in column A:

John M. Ford
Albert Cushy
Bertland Sunderland
Hugo Sanchez d'Angelo


My idea is to find how many spaces there is until the first " "
character and then use the mid function until this character (" ") to
create a new string with only the first name in it.

Anyone know how create a synthax like this?

Gabe.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default How do I extract a word from a string?

Charlie's code is great...in this instance.

I found at times that VBA has problems with referencing 'a space' using
double quotes. Try creating the following and you'll get an error:

range("a1").formula = "=if(activesheet.name = "Sheet1",True, " ")"

By trying to input or search for a space using " " within a quoted line, VBA
crashes. For conistency I use the reference to Char(32), the ASCII reference
to a space.

Charlie's example would now look like the following:

FirstName = Left(FullName, InStr(FullName, Char(32)) - 1)


"Charlie" wrote:

FirstName = Left(FullName, InStr(FullName, " ") - 1)


"Gabe Tiger" wrote:

EG.

I want to only get the first name of the following full names that are
given in column A:

John M. Ford
Albert Cushy
Bertland Sunderland
Hugo Sanchez d'Angelo


My idea is to find how many spaces there is until the first " "
character and then use the mid function until this character (" ") to
create a new string with only the first name in it.

Anyone know how create a synthax like this?

Gabe.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default How do I extract a word from a string?

The error comes from the fact that the formula has nested
quotes. It should be written as
Range("a1").Formula = _
"=if(activesheet.name = ""Sheet1"",True, "" "")"

(line split to avoid wrapping)

I've never known VBA to have any problems with spaces, and
for reasons of readability I'd strongly recommend against
using Chr(32) (not Char(32) BTW)


SyrNO wrote:
Charlie's code is great...in this instance.

I found at times that VBA has problems with referencing 'a space' using
double quotes. Try creating the following and you'll get an error:

range("a1").formula = "=if(activesheet.name = "Sheet1",True, " ")"

By trying to input or search for a space using " " within a quoted line, VBA
crashes. For conistency I use the reference to Char(32), the ASCII reference
to a space.

Charlie's example would now look like the following:

FirstName = Left(FullName, InStr(FullName, Char(32)) - 1)


"Charlie" wrote:

FirstName = Left(FullName, InStr(FullName, " ") - 1)


"Gabe Tiger" wrote:

EG.

I want to only get the first name of the following full names that are
given in column A:

John M. Ford
Albert Cushy
Bertland Sunderland
Hugo Sanchez d'Angelo


My idea is to find how many spaces there is until the first " "
character and then use the mid function until this character (" ") to
create a new string with only the first name in it.

Anyone know how create a synthax like this?

Gabe.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default How do I extract a word from a string?

Problems with " " (literal space)? Me neither, but I do often come across
programmers who haven't yet discovered the "double-up" requirement. It
applies to apostrophes also, in the event one wants to reference a sheet name
that contains an apostrophe:

Sheet.Name = "Don't"

Range("'Don''t'!A1")="""Ok, I Won't"""


"Andrew Taylor" wrote:

The error comes from the fact that the formula has nested
quotes. It should be written as
Range("a1").Formula = _
"=if(activesheet.name = ""Sheet1"",True, "" "")"

(line split to avoid wrapping)

I've never known VBA to have any problems with spaces, and
for reasons of readability I'd strongly recommend against
using Chr(32) (not Char(32) BTW)


SyrNO wrote:
Charlie's code is great...in this instance.

I found at times that VBA has problems with referencing 'a space' using
double quotes. Try creating the following and you'll get an error:

range("a1").formula = "=if(activesheet.name = "Sheet1",True, " ")"

By trying to input or search for a space using " " within a quoted line, VBA
crashes. For conistency I use the reference to Char(32), the ASCII reference
to a space.

Charlie's example would now look like the following:

FirstName = Left(FullName, InStr(FullName, Char(32)) - 1)


"Charlie" wrote:

FirstName = Left(FullName, InStr(FullName, " ") - 1)


"Gabe Tiger" wrote:

EG.

I want to only get the first name of the following full names that are
given in column A:

John M. Ford
Albert Cushy
Bertland Sunderland
Hugo Sanchez d'Angelo


My idea is to find how many spaces there is until the first " "
character and then use the mid function until this character (" ") to
create a new string with only the first name in it.

Anyone know how create a synthax like this?

Gabe.




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
Formula to extract a specific word from text string Dinesh Excel Worksheet Functions 4 November 3rd 06 08:35 PM
Extract from string mark Excel Discussion (Misc queries) 2 August 8th 06 12:38 PM
extract string owl527[_7_] Excel Programming 3 November 4th 05 10:35 AM
Extract sub string sixbeforedawn Excel Worksheet Functions 2 October 24th 05 09:50 AM
extract string dreamer[_17_] Excel Programming 2 June 1st 04 12:50 PM


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

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"