GET value into the string

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

GET value into the string

Post by sal21 »

i have this string:

asdds asdasdasd asdasdsad aasdddddd EURO 53,000 D
ssssss aaaaas eeee EURO 53,000 D

i need to get the trimmed value between EURO and D

note:
The lenght of string is variable... but into the string are present always EURO and D

User avatar
HansV
Administrator
Posts: 78561
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: GET value into the string

Post by HansV »

Let's say the string is named strValue.

' Declare variables
Dim p1 As Long
Dim p2 As Long
Dim strReturn As String

' Find position of "EURO"
p1 = InStr(strValue, "EURO")
' Find position of "D" after "EURO"
p2 = InStr(p1 + 4, strValue, "D")
' Extract value
strReturn = Mid(strValue, p1 + 4, p2 - p1 - 4)
' Trim the string
strReturn = Trim(strReturn)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: GET value into the string

Post by sal21 »

HansV wrote:Let's say the string is named strValue.

' Declare variables
Dim p1 As Long
Dim p2 As Long
Dim strReturn As String

' Find position of "EURO"
p1 = InStr(strValue, "EURO")
' Find position of "D" after "EURO"
p2 = InStr(p1 + 4, strValue, "D")
' Extract value
strReturn = Mid(strValue, p1 + 4, p2 - p1 - 4)
' Trim the string
strReturn = Trim(strReturn)
strugged arround 2 days!!!!!!!!!!!!!!!!!!!!!! :hairout: :scratch:
Now with Hans anly a minutes! tath is very good :thankyou: :clapping: :clapping: :clapping: :clapping: :clapping: :clapping:

but.... i can use this similar function or to speed up the code i can incapsulate into the code?

Note:
The string is a result of loop from txt file with approx 45.xxx lines

User avatar
HansV
Administrator
Posts: 78561
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: GET value into the string

Post by HansV »

You can use this code within other code, of course, but I don't know whether it will speed up that code - that depends on what you're currently doing with the lines.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: GET value into the string

Post by sal21 »

HansV wrote:You can use this code within other code, of course, but I don't know whether it will speed up that code - that depends on what you're currently doing with the lines.
OK! i thinking and decide.... :scratch:

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: GET value into the string

Post by sal21 »

HansV wrote:Let's say the string is named strValue.

' Declare variables
Dim p1 As Long
Dim p2 As Long
Dim strReturn As String

' Find position of "EURO"
p1 = InStr(strValue, "EURO")
' Find position of "D" after "EURO"
p2 = InStr(p1 + 4, strValue, "D")
' Extract value
strReturn = Mid(strValue, p1 + 4, p2 - p1 - 4)
' Trim the string
strReturn = Trim(strReturn)
PROB!

"D" is dinamic is possible ican have DD, C ...

EURO is static :grin:

Note:
Explain me +4 and -4
tks for patience

User avatar
HansV
Administrator
Posts: 78561
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: GET value into the string

Post by HansV »

The +4 is the length of the string "EURO". We have to add this to the position of the string to find the text after it.
Will there always be a space between the number and the letter(s) after it?
Best wishes,
Hans