Kanwaljit Singh Dhunna
asked on
VBA Equivalent of a Formula
Hi Experts,
What is the VBA Equivalent of the following Formula in Excel
=RIGHT(E2,FIND("_",E2,1)-1 )
What is the VBA Equivalent of the following Formula in Excel
=RIGHT(E2,FIND("_",E2,1)-1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Byundt,
That worked for me. Nice to hear from you after a loooong time....
Regards
Kanwal
That worked for me. Nice to hear from you after a loooong time....
Regards
Kanwal
In VBA when you have to search from the right, there is a VBA function called InStrRev. So you can use InStrRev also.
Right([E2], InStrRev([E2], "_") - 1)
ASKER
VBA function called InStrRev. So you can use InStrRev also.Both are InStrRev, What is the difference ?
Neeraj brings up a good point: does your worksheet formula give you the answer you are looking for?
I tested the various VBA methods with the test string "donkey_ote" and they all returned "ey_ote"
If you want the part that comes after the underscore, you might use:
I tested the various VBA methods with the test string "donkey_ote" and they all returned "ey_ote"
If you want the part that comes after the underscore, you might use:
Split([E2],"_")(1) 'If there is only one underscore
Mid([E2],InStr(1,[E2],"_")+1)
If you want to include the underscore, then you might use:"_" & Split([E2],"_")(1) 'If there is only one underscore
Mid([E2],InStr(1,[E2],"_"))
ASKER
I used the following. My original Excel formula was wrong and so was its VBA Equivalent.
Target.Value = Right(Target.Value, Len(Target.Value) - InStrRev(Target.Value, "_"))
InStr looks from the beginning of the string, and the first parameter is the starting position. InStr(1,"abc_def_ghi","_") returns 4 for the position of the first underscore character in the test string.
InStrRev looks from the end and does not use a starting position parameter. InStrRev("abc_def_ghi","_" ) returns 8 for the position of the last underscore character in the test string.
If there is only one underscore, InStr and InStrRev return the same value.
InStrRev looks from the end and does not use a starting position parameter. InStrRev("abc_def_ghi","_"
If there is only one underscore, InStr and InStrRev return the same value.
ASKER
Honestly, I could not understand the Split method.
Not sure whether It will work for me or not !
Not sure whether It will work for me or not !
ASKER
If there is only one underscore, InStr and InStrRev return the same value.
Got it....
Split([E2],"_") breaks the text up into pieces, splitting the text each time it finds an underscore. It returns an array of values, so I followed the Split function with an array subscript in parentheses.
Split([E2],"_")(0) returns the first element (the part before the first underscore); it returns the entire string if there is no underscore. Split([E2],"_")(1) returns the second element; it returns all the text to the right of the underscore if there is only one such character in cell E2.
Split([E2],"_")(0) returns the first element (the part before the first underscore); it returns the entire string if there is no underscore. Split([E2],"_")(1) returns the second element; it returns all the text to the right of the underscore if there is only one such character in cell E2.
ASKER
NICE EXPLANATION !
Tried it and it worked like a Charm.
Thanks Sir !!!!!!!
Tried it and it worked like a Charm.
Thanks Sir !!!!!!!
Open in new window