Results 1 to 6 of 6

Thread: Using Regular Expression than Substring in Microsoft Excel

  1. #1
    Join Date
    Oct 2011
    Posts
    75

    Using Regular Expression than Substring in Microsoft Excel

    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 compare against specifc characters. Even better, can I use regular expressions.
    Code:
    str = ActiveSheet.Cells(r, c)
    check = substr(str, 1, 3)
    If (check = "USA") Then
    ' do something
    End If

  2. #2
    Join Date
    Jul 2011
    Posts
    440

    Re: Using Regular Expression than Substring in Microsoft Excel

    Try this out.
    Code:
    Dim sStr As String
    sStr = ActiveSheet.Cells(r, c).Value
    If Left(sStr, 3) = "USA" Then
    'Do something
    End If

  3. #3
    Join Date
    Jul 2011
    Posts
    434

    Re: Using Regular Expression than Substring in Microsoft Excel

    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"

  4. #4
    Join Date
    Oct 2011
    Posts
    75

    Re: Using Regular Expression than Substring in Microsoft Excel

    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 , tr1 = ActiveSheet.Cells(r, c1). If Left(str1, 3) = "USA" Then do something Next End If do something.

  5. #5
    Join Date
    Aug 2011
    Posts
    460

    Re: Using Regular Expression than Substring in Microsoft Excel

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

  6. #6
    Join Date
    Aug 2011
    Posts
    564

    Re: Using Regular Expression than Substring in Microsoft Excel

    Try something like this :
    Code:
    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

Similar Threads

  1. Replies: 2
    Last Post: 02-02-2012, 04:46 PM
  2. Regular expression Help in asp.net for DOB.
    By maheshmartha in forum Software Development
    Replies: 2
    Last Post: 06-03-2010, 07:06 AM
  3. Regular expression in MySQL
    By Fragman in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 10:15 PM
  4. Regular expression in Perl
    By Ameeryan in forum Software Development
    Replies: 2
    Last Post: 19-11-2009, 02:07 PM
  5. Replies: 2
    Last Post: 03-09-2009, 01:23 PM

Tags for this Thread

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
  •  
Page generated in 1,713,410,029.31546 seconds with 17 queries