0
abc = Sheets("02").Range("calc02").Value  // value is: Rabat 5%
xR = Mid(abc, 7, 1) // Result is: 5

This is ok, but what if the content is: Rabat 50%, or Rabat 100%
I want to extract this number only, whether it is one, two or three-digit.

1
  • Does your string always contain the numeric portion at the end? Commented Aug 21, 2012 at 6:26

3 Answers 3

2

Use a regular expression:

var number = Regex.Match(abc, "[0-9]+");
Sign up to request clarification or add additional context in comments.

1 Comment

But it could work with some syntax changes: macrostash.com/2011/10/08/…
1
xR = Mid$(abc, 7)  ' Returns 5% or 50% etc
xR = Left$(xr, Len(xR)) - 1)  ' strips the %

or, if you want xR returned as a number

xR = Val(Mid$(abc, 7))

Comments

1

What about:

xR = Mid(abc, 7, Len(abc)-7)

Assuming the string is always the same, this takes the length of the string into consideration to dynamically return the value. This will only work with that format, but that sounds like what you want :)

If you instead wanted the percentage AND the word(s) in front were if variable length, this may work:

percent = Right(abc, InStr(StrReverse(abc)," ")-1)
xR = Mid(percent, 1, Len(percent)-1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.