+ Reply to Thread
Results 1 to 9 of 9

Substring in excel? How about regular expressions?

  1. #1
    Samuel
    Guest

    Substring in excel? How about regular expressions?

    I need to act upon the test "USA" found in the first three chars of a
    cell.
    This is what I did, but I get a compile error..
    So I did something wrong. How do I copare against specifc characters?
    Even better, can I use regular expressions?

    str = ActiveSheet.Cells(r, c)
    check = substr(str, 1, 3)
    If (check = "USA") Then
    ' do something
    End If


  2. #2
    Norman Jones
    Guest

    Re: Substring in excel? How about regular expressions?

    Hi Samuel,

    Try:

    Dim sStr As String

    sStr = ActiveSheet.Cells(r, c).Value

    If Left(sStr, 3) = "USA" Then
    'Do something
    End If


    ---
    Regards,
    Norman



    "Samuel" <[email protected]> wrote in message
    news:[email protected]...
    >I need to act upon the test "USA" found in the first three chars of a
    > cell.
    > This is what I did, but I get a compile error..
    > So I did something wrong. How do I copare against specifc characters?
    > Even better, can I use regular expressions?
    >
    > str = ActiveSheet.Cells(r, c)
    > check = substr(str, 1, 3)
    > If (check = "USA") Then
    > ' do something
    > End If
    >




  3. #3
    Miguel Zapico
    Guest

    RE: Substring in excel? How about regular expressions?

    The compile error may be because of the name of the variable "str", try with
    str1, for example.
    About functions to play with text, try LEFT, MID or RIGHT. In this case,
    LEFT can be the best one.

    You can use Regular Expresions in VBA, just add the reference for "Microsoft
    VBScript Regular Expresions 5.5"

    Hope this helps,
    Miguel.

    "Samuel" wrote:

    > I need to act upon the test "USA" found in the first three chars of a
    > cell.
    > This is what I did, but I get a compile error..
    > So I did something wrong. How do I copare against specifc characters?
    > Even better, can I use regular expressions?
    >
    > str = ActiveSheet.Cells(r, c)
    > check = substr(str, 1, 3)
    > If (check = "USA") Then
    > ' do something
    > End If
    >
    >


  4. #4
    Samuel
    Guest

    Re: Substring in excel? How about regular expressions?

    Thank you, that gets rid of that problem, but brings up a second one -
    How do I break out of a loop?
    If I have the following, I now get a compile error (next without for)
    on the new "next' added within the new USA compare...How do I break
    out?
    For i = 1 To lastrow
    str1 = ActiveSheet.Cells(r, c1)
    If Left(str1, 3) = "USA" Then
    ...do something
    Next
    End If
    ...do something
    Next


  5. #5
    Miguel Zapico
    Guest

    Re: Substring in excel? How about regular expressions?

    You can break the loop with an EXIT FOR statement inside the IF construction.

    Miguel.

    "Samuel" wrote:

    > Thank you, that gets rid of that problem, but brings up a second one -
    > How do I break out of a loop?
    > If I have the following, I now get a compile error (next without for)
    > on the new "next' added within the new USA compare...How do I break
    > out?
    > For i = 1 To lastrow
    > str1 = ActiveSheet.Cells(r, c1)
    > If Left(str1, 3) = "USA" Then
    > ...do something
    > Next
    > End If
    > ...do something
    > Next
    >
    >


  6. #6
    Samuel
    Guest

    Re: Substring in excel? How about regular expressions?

    I'm sorry, Miguel, but I misphrased it. I want to continue with the
    next iteration. Not break out complelty.
    If I change the 'next' to a 'next for' i get the same error.


  7. #7
    Miguel Zapico
    Guest

    Re: Substring in excel? How about regular expressions?

    I am not sure if you can jump to the next iteration on VBA, maybe you can ask
    the question on the excel.programming newsgroup.

    Sorry for not being able to help more,
    Miguel.

    "Samuel" wrote:

    > I'm sorry, Miguel, but I misphrased it. I want to continue with the
    > next iteration. Not break out complelty.
    > If I change the 'next' to a 'next for' i get the same error.
    >
    >


  8. #8
    Samuel
    Guest

    Re: Substring in excel? How about regular expressions?

    you were great! thank you!


  9. #9
    Norman Jones
    Guest

    Re: Substring in excel? How about regular expressions?

    Hi Samuel,

    Try something like:

    '=============>>
    Public Sub Tester()
    Dim sStr As String
    Dim i As Long
    Dim LastRow As Long

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    For i = 1 To LastRow
    sStr = ActiveSheet.Cells(i, "A").Value
    If Left(sStr, 3) = "USA" Then
    'Do something, e.g.:
    Cells(i, "A").Interior.ColorIndex = 6
    Else
    'Do something else
    End If
    Next i

    End Sub
    '<<=============


    ---
    Regards,
    Norman



    "Samuel" <[email protected]> wrote in message
    news:[email protected]...
    > I'm sorry, Miguel, but I misphrased it. I want to continue with the
    > next iteration. Not break out complelty.
    > If I change the 'next' to a 'next for' i get the same error.
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1