Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Instr function problem

Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I need to
find the final "a" at the first attempt in a loop. Can you help please?
Thanks,
Ross


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Instr function problem

Ross,

Use the InStrRev function. E.g.,

Debug.Print InStrRev(Range("A1"), "a", -1, vbTextCompare)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Ross Withey" wrote in message
...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I need

to
find the final "a" at the first attempt in a loop. Can you help please?
Thanks,
Ross




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default Instr function problem

Ross,

Depending on your version of excel you might have
InStrRev

InStrRev(Range("A1"), "a") will return 6 if A1 is banana

Dan E

"Ross Withey" wrote in message ...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I need to
find the final "a" at the first attempt in a loop. Can you help please?
Thanks,
Ross




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Instr function problem

Sorry, I forgot to say I'm on Excel 97 which does not recognise InstRev. Do
I need a custom function?
Thank you!
Ross.

"Chip Pearson" wrote in message
...
Ross,

Use the InStrRev function. E.g.,

Debug.Print InStrRev(Range("A1"), "a", -1, vbTextCompare)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Ross Withey" wrote in message
...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I

need
to
find the final "a" at the first attempt in a loop. Can you help please?
Thanks,
Ross






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Instr function problem

Gentlemen,
I have come up with this to reverse a range, so that Instr could then be
used. It is not very elegant, though. Can you think of anything better?
Ross

Sub Reversit()
x = Len(Range("A1")) + 1
For r = 1 To x
Range("A2") = Mid(Range("A1"), r, 1) & Range("A2")
Next
End Sub



"Dan E" wrote in message
...
Ross,

Depending on your version of excel you might have
InStrRev

InStrRev(Range("A1"), "a") will return 6 if A1 is banana

Dan E

"Ross Withey" wrote in message

...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I

need to
find the final "a" at the first attempt in a loop. Can you help please?
Thanks,
Ross








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Instr function problem

fruitName = "Bananas"
For t = 0 To Len(fruitName)
If InStr(Len(fruitName) - t, fruitName, "a") Then Exit For
Next t
MsgBox Len(fruitName) - t


Ross Withey wrote in message
...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I need

to
find the final "a" at the first attempt in a loop. Can you help please?
Thanks,
Ross




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Instr function problem

Stuart,
Thanks -that looks elegant!
Ross

"Stuart" wrote in message
...
fruitName = "Bananas"
For t = 0 To Len(fruitName)
If InStr(Len(fruitName) - t, fruitName, "a") Then Exit For
Next t
MsgBox Len(fruitName) - t


Ross Withey wrote in message
...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I

need
to
find the final "a" at the first attempt in a loop. Can you help please?
Thanks,
Ross






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default Instr function problem

yes, use this

Function InStrRev(Strng As String, Char As String) As Integer
Dim Lngth As Integer, i As Integer
Lngth = Len(Strng)
For i = Lngth To 1 Step -1
If Mid(Strng, i, 1) = Char Then
InStrRev = i
Exit Function
End If
Next i
End Function
'Howard Groves cmmroom@ ddre.detroitdiesel.com

--
Don Guillett
SalesAid Software

"Ross Withey" wrote in message
...
Sorry, I forgot to say I'm on Excel 97 which does not recognise InstRev.

Do
I need a custom function?
Thank you!
Ross.

"Chip Pearson" wrote in message
...
Ross,

Use the InStrRev function. E.g.,

Debug.Print InStrRev(Range("A1"), "a", -1, vbTextCompare)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Ross Withey" wrote in message
...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want

to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I

need
to
find the final "a" at the first attempt in a loop. Can you help

please?
Thanks,
Ross








  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Instr function problem

Ross

unless you can guarantee that the cell/string will contain what you are
looking for, you may need to modify the code slightly:

Sub InstrTest()
'fruitName = "Bananas"
fruitName = "Melon"
For t = 0 To Len(fruitName)
On Error Resume Next
If InStr(Len(fruitName) - t, LCase(fruitName), "a") Then Exit For
On Error GoTo 0
Next t
If t = Len(fruitName) Then
MsgBox "Not found"
Else
MsgBox Len(fruitName) - t
End If
End Sub

And you probably need to check the case if it's an alpha character you're
looking for.

Regards

Trevor


"Ross Withey" wrote in message
...
Stuart,
Thanks -that looks elegant!
Ross

"Stuart" wrote in message
...
fruitName = "Bananas"
For t = 0 To Len(fruitName)
If InStr(Len(fruitName) - t, fruitName, "a") Then Exit For
Next t
MsgBox Len(fruitName) - t


Ross Withey wrote in message
...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I want

to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but I

need
to
find the final "a" at the first attempt in a loop. Can you help

please?
Thanks,
Ross








  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Instr function problem

Did for me. Run-time error '5': Invalid procedure call or argument.

on "If InStr(Len(fruitName) - t, LCase(fruitName), "a") Then "


"Stuart" wrote in message
...
Not finding the characture does not cause an error!



Trevor Shuttleworth wrote in message
...
Ross

unless you can guarantee that the cell/string will contain what you are
looking for, you may need to modify the code slightly:

Sub InstrTest()
'fruitName = "Bananas"
fruitName = "Melon"
For t = 0 To Len(fruitName)
On Error Resume Next
If InStr(Len(fruitName) - t, LCase(fruitName), "a") Then Exit For
On Error GoTo 0
Next t
If t = Len(fruitName) Then
MsgBox "Not found"
Else
MsgBox Len(fruitName) - t
End If
End Sub

And you probably need to check the case if it's an alpha character

you're
looking for.

Regards

Trevor


"Ross Withey" wrote in message
...
Stuart,
Thanks -that looks elegant!
Ross

"Stuart" wrote in message
...
fruitName = "Bananas"
For t = 0 To Len(fruitName)
If InStr(Len(fruitName) - t, fruitName, "a") Then Exit For
Next t
MsgBox Len(fruitName) - t


Ross Withey wrote in message
...
Hello,
If cell A1 has the string value "Bananas"

Instr(1,"Range("A1"), "a") finds the first "a". My problem is, I

want
to
find the final "a".
Can you suggest the right code? I know I can increase the "1", but

I
need
to
find the final "a" at the first attempt in a loop. Can you help

please?
Thanks,
Ross












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
Use InStr function in formula? Lee Hunter Excel Worksheet Functions 8 May 8th 23 03:45 AM
is there an equal fxn for 'InStr' in excel. Not Find or Search Clausius Excel Worksheet Functions 3 May 1st 23 03:41 AM
Help with VBA InStr() function EagleOne Excel Discussion (Misc queries) 10 April 12th 07 02:47 PM
Where is the Instr() function in Excel 2003? chem21 Excel Discussion (Misc queries) 3 October 9th 06 03:49 PM
InStr used in SQL query dchow Excel Programming 3 July 21st 03 09:09 AM


All times are GMT +1. The time now is 06:54 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"