Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Extract last name into a variable using VBA code


Please help with VBA code that extracts the last name from a cell
containing First name, sometimes middle initial or name, & last name
and put into a variable called LastName.

For example in cell A1 is the name John B Doe

Need LastName variable to contain the word Doe.

Thank you so very much. mikeburg


--
mikeburg
------------------------------------------------------------------------
mikeburg's Profile: http://www.excelforum.com/member.php...o&userid=24581
View this thread: http://www.excelforum.com/showthread...hreadid=503728

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Extract last name into a variable using VBA code

Hi Mike,

For xl2k+, try:

'=============
Public Sub Tester001()
Dim sStr As String
Dim Pos As Long

sStr = Range("A1").Value

Pos = InStrRev(sStr, " ")

sStr = Mid(sStr, Pos + 1)

MsgBox sStr

End Sub
'<<=============


---
Regards,
Norman



"mikeburg" wrote in
message ...

Please help with VBA code that extracts the last name from a cell
containing First name, sometimes middle initial or name, & last name
and put into a variable called LastName.

For example in cell A1 is the name John B Doe

Need LastName variable to contain the word Doe.

Thank you so very much. mikeburg


--
mikeburg
------------------------------------------------------------------------
mikeburg's Profile:
http://www.excelforum.com/member.php...o&userid=24581
View this thread: http://www.excelforum.com/showthread...hreadid=503728



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Extract last name into a variable using VBA code


Wonderful!

How would you extract the first name using the simplest VBA code?

Thanks, mikeburg


--
mikeburg
------------------------------------------------------------------------
mikeburg's Profile: http://www.excelforum.com/member.php...o&userid=24581
View this thread: http://www.excelforum.com/showthread...hreadid=503728

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Extract last name into a variable using VBA code

here's one way if the name is in a1


dim fname as string
sub fname()
Range("a2").Value = Left(Range("a1"), InStr(1, Range("a1"), " ") - 1)
end sub
--


Gary


"mikeburg" wrote in
message ...

Wonderful!

How would you extract the first name using the simplest VBA code?

Thanks, mikeburg


--
mikeburg
------------------------------------------------------------------------
mikeburg's Profile:
http://www.excelforum.com/member.php...o&userid=24581
View this thread: http://www.excelforum.com/showthread...hreadid=503728



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Extract last name into a variable using VBA code

Sub foo()
Dim LastName As String
Dim Temp

Temp = Split(ActiveCell)

LastName = Temp(UBound(Temp))
FirstName = Temp(LBound(Temp))

Debug.Print LastName, FirstName

End Sub

--
Regards,
Tom Ogilvy


"mikeburg" wrote in
message ...

Wonderful!

How would you extract the first name using the simplest VBA code?

Thanks, mikeburg


--
mikeburg
------------------------------------------------------------------------
mikeburg's Profile:

http://www.excelforum.com/member.php...o&userid=24581
View this thread: http://www.excelforum.com/showthread...hreadid=503728





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default Extract last name into a variable using VBA code

Tom Ogilvy wrote

Debug.Print LastName, FirstName


I've often seen this syntax for testing. Where do the results actually show
up? I ran the sub from one of my workbooks and didn't see any result.

--
David
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Extract last name into a variable using VBA code

In the VBE, go to view and select immediate window.

That is where they show up. Also, like the name says, it is a good place to
immediately execute instructions. For example. go to the immediate window
and enter

Range("B9:F10").Interior.ColorIndex = 3 <cr

and the active sheet should be colored as indicated.

Suppose you were constructing a complex string and wanted to test it.

v = 2 <cr
y = 6 <cr
? y & "/" & v & " = " & y/v <cr

The question mark is equivalent to debug.print, so it evaluates the
expression and displays the results.

Very useful.

--
Regards,
Tom Ogilvy



"David" wrote in message
...
Tom Ogilvy wrote

Debug.Print LastName, FirstName


I've often seen this syntax for testing. Where do the results actually

show
up? I ran the sub from one of my workbooks and didn't see any result.

--
David



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Extract last name into a variable using VBA code

On Sun, 22 Jan 2006 01:08:26 -0600, mikeburg
wrote:


Wonderful!

How would you extract the first name using the simplest VBA code?

Thanks, mikeburg


Basically the same macro:

VBA6+

======================
Option Explicit

Sub foo()
Dim LastName As String, FirstName as String
Dim Temp

Temp = Split(ActiveCell)

LastName = Temp(UBound(Temp))
FirstName = Temp(LBound(Temp))

Debug.Print FirstName
Debug.Print LastName

End Sub
======================

Pre VBA6 you would just need to find the first space, and then use the Left
function.


--ron
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default Extract last name into a variable using VBA code

Tom Ogilvy wrote

In the VBE, go to view and select immediate window.

That is where they show up. Also, like the name says, it is a good
place to immediately execute instructions. For example. go to the
immediate window and enter

Range("B9:F10").Interior.ColorIndex = 3 <cr

and the active sheet should be colored as indicated.

Suppose you were constructing a complex string and wanted to test it.

v = 2 <cr
y = 6 <cr
? y & "/" & v & " = " & y/v <cr

The question mark is equivalent to debug.print, so it evaluates the
expression and displays the results.

Very useful.


Ah, yes. Thank you. I've often used the immediate window to test results
with ?, but didn't know debug.print was its equivalent in code - until now.

--
David
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
Extract numbers from a code Mark Excel Worksheet Functions 3 June 9th 09 04:31 PM
can i link a variable cost code with a variable sum I need help!! Excel Discussion (Misc queries) 0 August 1st 08 11:40 AM
Extract a number from a variable text string tipsy Excel Discussion (Misc queries) 4 May 4th 08 03:28 AM
Code Post: Extract Trendline coefficients who Excel Discussion (Misc queries) 2 January 10th 05 11:36 PM
Extract a cells format and apply to a variable MikeR[_2_] Excel Programming 7 October 22nd 03 01:39 PM


All times are GMT +1. The time now is 01:53 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"