Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Extracting the Nth element from a String

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan - 17 Mar 2008 16:08 GMT
I am using John Walkenbach function to extract the Nth element from a string:
this is working fine in most cases bit not when one of my Separator is
char(10) ---> the end of a line, any idea?

Many thanks,
Dan

Function EXTRACTELEMENT(Txt, n, Separator) As String
'   Returns the nth element of a text string, where the
'   elements are separated by a specified separator character
   Dim AllElements As Variant
   AllElements = Split(Txt, Separator)
   EXTRACTELEMENT = AllElements(n - 1)
End Function
Peter T - 17 Mar 2008 16:32 GMT
Maybe this will explain

Sub test()
Dim i As Long, s As string

s = "A" & vbCr & "B" & vbLf & "C" & vbNewLine & "D"
For i = 1 To Len(s)
Debug.Print i, Asc(Mid(s, i, 1))
Next

s = Replace(s, vbCr, vbLf)
s = Replace(s, vbLf & vbLf, vbLf)

Debug.Print
For i = 1 To Len(s)
Debug.Print i, Asc(Mid(s, i, 1))
Next

s = Replace(s, vbCr, vbLf)

arr = Split(s, Chr(10))
For i = 0 To UBound(arr)
Debug.Print arr(i)
Next

End Sub

Ctrl-g to view the Immediate window

Of course don't use as written if your string might have double line breaks.

Regards,
Peter T

> I am using John Walkenbach function to extract the Nth element from a string:
> this is working fine in most cases bit not when one of my Separator is
[quoted text clipped - 10 lines]
>     EXTRACTELEMENT = AllElements(n - 1)
> End Function
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.