How to loop a string?

atf32

Board Regular
Joined
Apr 13, 2011
Messages
157
I need to loop a string and to do the following:
  1. Determine if one of these characters ( "/", " [", " (" ) exist in the string.
  2. If one of the above strings do exist, then I want to return all of the string to the left of the targeted characters ( "/", " [", " (" ).
  3. If none of the targeted characters exist, then return the full string.
:confused:
 
UDF alternative.
Code:
[COLOR="Blue"]Function[/COLOR] ToLeft(Str [COLOR="Blue"]As[/COLOR] String) [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    [COLOR="Blue"]With[/COLOR] CreateObject("VBScript.RegExp")
        .Pattern = "(/|\[|\()"
        ToLeft = Left(Str, .Execute(Str)(0).FirstIndex)
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Sektor,

I *think* you need to check if any match was founded, like

Code:
Function ToLeft(Str As String) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "(/|\[|\()"
        If .Execute(Str).Count > 0 Then
            ToLeft = Left(Str, .Execute(Str)(0).FirstIndex)
        Else
            ToLeft = Str
        End If
    End With
End Function

M.
 
Upvote 0
Here is another UDF for you to consider (it is a non-RegEx solution if that matters to you)...
Code:
Function ToLeft(Text As String) As String
  Dim X As Long
  For X = 1 To Len(Text)
    If Mid(Text, X, 1) Like "[/[(]" Then
      ToLeft = Left(Text, X - 1)
      Exit For
    End If
  Next
End Function
 
Upvote 0
Sektor,

I *think* you need to check if any match was founded, like

Code:
Function ToLeft(Str As String) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "(/|\[|\()"
        If .Execute(Str).Count > 0 Then
            ToLeft = Left(Str, .Execute(Str)(0).FirstIndex)
        Else
            ToLeft = Str
        End If
    End With
End Function

M.

Kwel I was thinking how to use VBScript.RegExp. Thank you for solution.

Biz
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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