Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default Extracting the Nth element from a String

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Extracting the Nth element from a String

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

"Dan" wrote in message
...
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



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
Can one store a string in a Array element? [email protected] Excel Discussion (Misc queries) 1 April 17th 07 12:53 PM
Extracting a string Peter Rooney Excel Discussion (Misc queries) 5 June 20th 06 06:34 PM
Delete String Element John[_88_] Excel Programming 3 February 8th 05 08:26 PM
Extracting nth element of X dimension ExcelMonkey[_69_] Excel Programming 1 February 4th 04 04:08 AM
Function to extract element of a complex delimited string (Pick D3 database) John Vinciguerra \(Chemform\) Excel Programming 2 November 17th 03 02:09 PM


All times are GMT +1. The time now is 07:20 AM.

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"