Get text after position in string

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to get the text after a string which can be found at any point in a cell.

The full string is 'VIDEO PRESENT: *YES/NO/NOT APPLICABLE'

What I need to extract is the answer, i.e. YES, NO or NOT APPLICABLE. The issue I have is that people have to delete the part of the answer which isn't relevant, leaving their answer, but I am getting all sorts of weird answers such as, *YES, /NO, *YES/ and any other variation you can think of!

So is there an efficient VBA method to grab the answer?
 
I think I see a problem with that. Would need 3 variables, not just i.
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
formula method

1681308814932.png


In B1
VBA Code:
=IF(NOT(ISERR(SEARCH("YES",A1))),"Yes",IF(NOT(ISERR(SEARCH("NOT AVAILABLE",A1))),"Not Available",IF(NOT(ISERR(SEARCH("NO",A1))),"No")))
 
Upvote 0
OK, this seems to work but due to lack of info about sheet name(s) or ranges those references would need to be dealt with
VBA Code:
Sub GetAnswer()
Dim i As Integer, x As Integer, y As Integer
Dim rng As Range

Set rng = Sheets("Sheet5").Range("A2")
i = InStr(rng, "YES")
x = InStr(rng, "NO")
y = InStr(rng, "NOT APPLICABLE")

Select Case True
     Case Is = i > 0 ' YES
          rng = Mid(rng, i, 3)
          
     Case Is = x > 0 And y = 0 ' NO
          rng = Mid(rng, x, 2)
          
     Case Is = y > 0 ' NOT APPLICABLE
          rng = Mid(rng, y, 14)

End Select

End Sub
 
Upvote 0
Thanks so much guys, a couple of things for me to try so I'll do some testing and come back to you.
 
Upvote 0
Sorry my solution won't be of any help as I had overlooked some detail from one of your previous posts.
 
Upvote 0
I realized that some issues can arise with that code so more checks are required. Those will also leave the entire string alone if no one edited a value (i.e. the cell value is VIDEO PRESENT: *YES/NO/NOT APPLICABLE). In that case you can be left with a yes or no if all tests are not made. Current version does nothing if that string is found.
VBA Code:
Sub GetAnswer()
Dim i As Integer, x As Integer, y As Integer
Dim Lrow As Long, a As Long
Dim rng As Range

On Error GoTo errHandler
Application.EnableEvents = False
Lrow = Range("A2").End(xlDown).Row
For a = 2 To Lrow ' 2 assumes there is one header row
     Set rng = Sheets("Sheet5").Range("A" & a)
     i = InStr(rng, "YES")
     x = InStr(rng, "NO")
     y = InStr(rng, "NOT APPLICABLE")
    
     Select Case True
          'if i,x,y are all >0 then the cell value was left as original so do nothing
          Case Is = i > 0 And x = 0 And y = 0 ' YES
              rng = Mid(rng, i, 3)
          Case Is = x > 0 And y = 0 And i = 0 ' NO
              rng = Mid(rng, x, 2)
          Case Is = y > 0 And x = 0 And i = 0 ' NOT APPLICABLE
              rng = Mid(rng, y, 14)
     End Select
Next
exitHere:
Set rng = Nothing
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere

End Sub
 
Upvote 0
Solution
OK, this seems to work but due to lack of info about sheet name(s) or ranges those references would need to be dealt with
VBA Code:
Sub GetAnswer()
Dim i As Integer, x As Integer, y As Integer
Dim rng As Range

Set rng = Sheets("Sheet5").Range("A2")
i = InStr(rng, "YES")
x = InStr(rng, "NO")
y = InStr(rng, "NOT APPLICABLE")

Select Case True
     Case Is = i > 0 ' YES
          rng = Mid(rng, i, 3)
         
     Case Is = x > 0 And y = 0 ' NO
          rng = Mid(rng, x, 2)
         
     Case Is = y > 0 ' NOT APPLICABLE
          rng = Mid(rng, y, 14)

End Select

End Sub
This one works just fine - thanks to all who took the time to respond, but Micron has the solution that works.
 
Upvote 0
Glad we could help & thanks for the recognition.
 
Upvote 0
As is usually the case I identify a further issue when I try to expand the solution to cover another scenario!

How could this work if the options are MALE/FEMALE?

I've tried to adapt the suggestion which works for the original question but it's not providing correct answers, I assume because FEMALE also contains MALE?
 
Upvote 0
If you look for male then yes, it will be found in "female". Using that method, now you need two more variables (for "male" and "female"), 2 more Case lines and add those 2 tests to every existing line.
 
Upvote 0

Forum statistics

Threads
1,215,778
Messages
6,126,841
Members
449,343
Latest member
DEWS2031

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