Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 33
Default Split values in cells to variables

I have a quick question

I need help with a basic macro that will enable me to search a cell
that has lastname and first name seperated by a comma (lastname,
firstname). All I need to do is create two variables that will only
have either the first name or last name. For example

Variable1 = Last name
Variable2 = First name


Once I am able to do this then I should be able to add it to my code

Thank you for all of your help in advance

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default Split values in cells to variables

It isn't clear if you need to (1) combine existing FirstName and LastName
variables before searching for the concatenation of variable, or (2) whether
you need to split the found result into FirstName and LastName.

For (1), use code like

Dim FoundCell As Range
Dim LastName As String
Dim FirstName As String
LastName = "pearson"
FirstName = "chip"

Set FoundCell = Range("A1:A10").Find(what:=LastName & ", " & FirstName, _
LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If FoundCell Is Nothing Then
Debug.Print "NOT FOUND"
Else
Debug.Print "FOUND AT: " & FoundCell.Address
End If


For (2), use code like:

Dim FoundCell As Range
Dim LastName As String
Dim FirstName As String
Dim VV As Variant
Set FoundCell = Range("A1:A10").Find(what:="pearson, chip", _
LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If FoundCell Is Nothing Then
Debug.Print "NOT FOUND"
Else
VV = Split(FoundCell.Text, ",")
LastName = Trim(VV(0))
FirstName = Trim(VV(1))
Debug.Print "Last Name: " & LastName, _
"First Name: " & FirstName
End If


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


wrote in message
...
I have a quick question

I need help with a basic macro that will enable me to search a cell
that has lastname and first name seperated by a comma (lastname,
firstname). All I need to do is create two variables that will only
have either the first name or last name. For example

Variable1 = Last name
Variable2 = First name


Once I am able to do this then I should be able to add it to my code

Thank you for all of your help in advance


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 87
Default Split values in cells to variables

Assuming you know how to loop through cells one possibility for the code
would be
firstName = Mid(mystring, 1, InStr(mystring, ",") - 1)
lastName = Mid(mystring, InStr(mystring, ",") + 1)
--
Best regards,
Edward


" wrote:

I have a quick question

I need help with a basic macro that will enable me to search a cell
that has lastname and first name seperated by a comma (lastname,
firstname). All I need to do is create two variables that will only
have either the first name or last name. For example

Variable1 = Last name
Variable2 = First name


Once I am able to do this then I should be able to add it to my code

Thank you for all of your help in advance


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Split values in cells to variables

I interpreted your question as wanting to parse ("search") a cell that you
know has the "last, first" string in it.

Dim words() As String
Dim variable1 As String
Dim variable2 As String

' Replace 'ActiveCell.Value' with your cell reference
words = Split(ActiveCell.Value, ",")

' Bounds check words() to be sure you have two words
' Trim() removes surrounding white space: " Bob" - "Bob"
variable1 = Trim(words(0))
variable2 = Trim(words(1))



" wrote:

I have a quick question

I need help with a basic macro that will enable me to search a cell
that has lastname and first name seperated by a comma (lastname,
firstname). All I need to do is create two variables that will only
have either the first name or last name. For example

Variable1 = Last name
Variable2 = First name


Once I am able to do this then I should be able to add it to my code

Thank you for all of your help in advance


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 33
Default Split values in cells to variables

On Jan 14, 5:21*pm, CjimO wrote:
I interpreted your question as wanting to parse ("search") a cell that you
know has the "last, first" string in it.

Dim words() As String
Dim variable1 As String
Dim variable2 As String

' Replace 'ActiveCell.Value' with your cell reference
words = Split(ActiveCell.Value, ",")

' Bounds check words() to be sure you have two words
' Trim() removes surrounding white space: *" Bob" - "Bob"
variable1 = Trim(words(0))
variable2 = Trim(words(1))



" wrote:
I have a quick question


I need help with a basic macro that will enable me to search a cell
that has lastname and first name seperated by a comma (lastname,
firstname). *All I need *to do is create two variables that will only
have either the first name or last name. *For example


Variable1 = Last name
Variable2 = First name


Once I am able to do this then I should be able to add it to my code


Thank you for all of your help in advance- Hide quoted text -


- Show quoted text -


Thank you. Everything worked out perfectly
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
VBA variables retaining their values GeorgeJ Excel Discussion (Misc queries) 2 August 17th 07 02:19 AM
how to look up values with two independent variables charlesfung Excel Worksheet Functions 4 June 17th 06 03:32 PM
Formula to Check Values between to variables John Excel Worksheet Functions 2 February 28th 06 10:30 PM
Chart two variables with differing values Dan Charts and Charting in Excel 2 January 23rd 06 07:07 PM
Split values from one cell to dif. cells saziz Excel Discussion (Misc queries) 2 September 30th 05 04:47 PM


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