0

I have a

String str = "abc#daso.xlx". 

I want to remove all characters in front of "#" so

str -> "#daso.xlx" . 

I can't use Left or Right function because the length of the string in front of "#" can be changed. I'm thinking to use Split() method but it would be the last choice if I can't find any others ways. I just don't want to use Array in VBA.

2 Answers 2

2

Use a combination of mid and instr

pos = InStr(yourString, "#")
newString = Mid(yourString, pos)

The first line finds the position of the first # in your string. The second asigns the substring from that position to the end of your string to a new string.

3
  • VBA questions are on topic for Super User - although more complicated ones are better off at Stack Overflow
    – Sathyajith Bhat
    Jun 12, 2013 at 9:03
  • oh I didn't know InStr return the index . Can I ask 1 more question ? what would have happened to the first line if my string don't contain "#" . Will it pop up error message or will it return some others value ? Thankks Sathya anw.
    – user224174
    Jun 13, 2013 at 5:35
  • InStr will return 0 if String2 isn't inside String1 (assuming the form InStr(String1, String2). As VBA strings aren't 0-indexed, checking if InStr returns 0 is a good way to check a few situations when passed only 2 strings. As well as String2 not being in String1, InStr returns 0 when: String1 is "" or Nothing and String2 is not found.
    – SBI
    Jun 13, 2013 at 6:00
-3

Try this

str$ = "abc#daso.xlx"

dim i%

i=instr(str, "#")

if i > 0 then str = mid(str, i)
1
  • 1
    You've effectively just repeated another answer. Aug 5, 2013 at 8:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .