0

I have a string with value "NA - Phase 1: Preliminary Phase" and I want to remove all the words before the dash "-".

Here is my current code, I am able to remove the string, but it keeps the "-", which I also want to remove.

indexOfThey = InStr(1, s, "-")
finalString = VBA.Mid(s, indexOfThey)
wksSourced.Cells(lastrowd + 1, 9).Value = finalString
3
  • If you know the dash is always followed by a space, you can just use Mid(s, indexOfThey + 2) Commented May 1, 2017 at 9:35
  • why don't use excel functions in VBA WorksheetFunction Library. Commented May 1, 2017 at 13:35
  • =RIGHT(G14,LEN(G14)-FIND("-",G14)) ' G14 is the cell referencing to the data Commented May 1, 2017 at 13:35

2 Answers 2

1

You can just replace it directly:

wksSourced.Cells(lastrowd + 1, 9).Replace "*- ", "", xlPart
Sign up to request clarification or add additional context in comments.

1 Comment

but I want to remove all the words Eg: "NA - " and not only "-"
0

I would want to make the modification of the cell contents conditional on there being a dash in it in the first place. I should also like to trim any blank spaces that may remain after the removal. Therefore I would modify your original code like this:-

n = InStr(S, "-")
If n Then wksSourced.Cells(lastrowd + 1, 9).Value = Trim(Mid(S, n + 1))

However, if the string S is already resident in the specified cell the same idea would be expressed like this:-

With wksSourced.Cells(lastrowd + 1, 9)
    n = InStr(.Value, "-")
    If n Then .Value = Trim(Mid(.Value, n + 1))
End With

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.