InstrRev

dugdugdug

Active Member
Joined
May 11, 2012
Messages
342
Code:
Dim MyString As String

MyString = "ApplesandOranges"

Dim MyResult As Integer

MyResult = InStrRev(MyString, "Or", -1)

MyResult returns 10.

Shouldn't it be 6 or 7 (because you're counting from the end backwards, so starting with s, then e, then n then .....
 
InstrRev looks backwards to find the search string then returns the number of characters from the start.
 
Upvote 0
I don't understand.

I thought it was looking for the letters Or, starting from the last letter in "ApplesandOranges".

"InstrRev looks backwards", looking at Or or ApplesandOranges backwards?
 
Upvote 0
It starts from the last letter of ApplesandOranges looking for Or. When it finds it, it returns the number of characters from the start ApplesandOranges
 
Upvote 0
Thanks.

Looking at this, VBScript InStrRev Function

the example

Rich (BB code):
txt="This is a beautiful day!"
response.write(InStrRev(txt,"i",-1) & "
")

returns 15 because it is starting from the end and finds the i in bold red, then counts from the start.

But this example:

Rich (BB code):
txt="This is a beautiful day!"
response.write(InStrRev(txt,"i",7) & "
")

returns 6.

The significance of the 7 means start from 7 (from the start or end)?

Either way, why is the answer 6?
 
Last edited:
Upvote 0
I assume that Start counts from the end (Help is no use on this). But the important point is that it searches backwards then returns the character count from the first character.
 
Upvote 0
I think I've got it finally!

The 7 means it starts from the beginning, ie the "T", counts 7 characters including spaces, to get to the "s" in "is", then counts right to left until it finds an "i", which in this case is the one in "is" and finally counts from the beginning to this "i", hence 6!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top