View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Rashid Khan Rashid Khan is offline
external usenet poster
 
Posts: 56
Default Function to extract values

Hi Mark,
Pardon me for my ignorance. But the suggestion u sent does not get the
result what I want.
Let me explain to u further.
The strings in Col AA appear either at the beginning in Col A or in between
somewhere always preceded by a space in Col A.

The suggestion u posted searches for either in the beginning with the value
= 1 but the 0 parts extracts part of the string as I already mentioned in
my previous post.

Hope u get it now
Rashid
"Mark Thorpe" wrote in message
...
Hi Rashid -

You should be able to check to see if the substring is at the beginning of
the longer string (InStr will return 1), and, if not, check to see if it
appears preceded by a space:

If InStr(sFullName, Cells(iRowAA, 27).Value) = 1 Then '

occurs
at beginning
sMatch = Cells(iRowAA, 27).Value
ElseIf InStr(sFullName, " " & Cells(iRowAA, 27).Value) 0

Then
' occurs preceded by space
sMatch = Cells(iRowAA, 27).Value
End If

"Rashid Khan" wrote in message
...
Hi Mark,
There is a slight problem with this macro.. It is finding everything

with
the InStr Function...
I need to find the string from within a string but it should from the
beginning of a word.. maybe the macro can search a space also.

Let me give u an example
My list has Mahad
so it is displaying from Gurumahadeo as Mahad... note that mahad is

within
this word Gurumahadeo.
The example u quoted earlier about Daniel and Danielle was ok with me as
long as it would extract the whole word from the beginning.. this macro
would extract Dan, Dani and Niel also.

How can we rectify this problem?
Any suggestion would be appreciated.
Rashid Khan

"Mark Thorpe" wrote in message
...
Hi Rashid -

This code should work for you. I tried to put in plenty of comments to

help
you learn more about Excel VBA:

Sub SearchNames()

Dim lRowCount As Long
Dim lRow As Long
Dim iRowAA As Integer
Dim sFullName As String
Dim sMatch As String ' match found in column AA

' Count number of used rows in the sheet:
lRowCount = ActiveSheet.UsedRange.Rows.Count

' Outer loop: look at each name in column A:
For lRow = 1 To lRowCount
sFullName = Cells(lRow, 1).Value
iRowAA = 1
sMatch = ""

' Inner loop: look at each name in column AA
' until either a match is found, or run out
' of names:
While Cells(iRowAA, 27).Value < "" And sMatch = ""
' InStr function returns value greater than
' zero if string2 is found within string1:
If InStr(sFullName, Cells(iRowAA, 27).Value) 0 Then
sMatch = Cells(iRowAA, 27).Value
End If
iRowAA = iRowAA + 1
Wend

' Place the match in column B:
Cells(lRow, 2).Value = sMatch
Next lRow


End Sub

Also, you might take a look at a series of articles called "Excel VBA:

No
Experience Necessary" on TechTrax online magazine. I'm not trying to
advertise, but you might find them helpful.
http://pubs.logicalexpressions.com/P...ssue.asp?ISI=0

Mark