+ Reply to Thread
Results 1 to 12 of 12

Search & Replace InString

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Search & Replace InString

    Hi all,

    I'm having somewhat of a difficult time trying to put some code together to
    search for a string of characters matching a specific arrangement.

    The specific arrange would be any numerical value followed by an alpha character.
    such as: (1abc,12abc,123abc, 1234abc, 1234nabc....) n= any numeral of any length
    abc = any alpha string of any length.

    I'd would like to start the search from the LEFT(1abc,1), and for the only the
    first case found,

    the a space " " needs to be put between the numeric and alpha characters.
    such as: (1 abc,12 abc,123 abc, 1234 abc, 1234 nabc....)
    I don't know if these would be easier using a "case select" code
    or something like

    Private Sub CommandButton2_Click()
    'For a text box imput
    
    Dim testLChar as string
    'Dim testMChar as string
    Dim testMChar1 as string
    Dim testMChar2 as string
    Dim N As Integer
    With Me.Txt_LOCATION
    N = 1
    testLChar = Left(.Value, N)
    testMChar1 = Mid(.Value, N)
    Do Until (testMChar1 Like "[0-9|A-Z]") = (testMChar2 Like "[A-Z_]")
    If (testLChar Like "[0-9-A-Z]") Then
    testMChar2 = Mid(.Value, N)
    N = N + 1
    If (testMChar2 Like "[A-Z]") Then
    testMChar2 = " " & testMChar2
    End If
    End If
    Loop
    End With
    end sub
    ...above code not working.

    Any help is appreciated.

    Thanks,

    BDB
    Last edited by bdb1974; 05-04-2010 at 09:24 AM.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Search & Replace InString

    You could try logic like this.

    Sub Test()
    TestString = "1234abcd"
    For N = 1 To Len(TestString)
        If IsNumeric(Left(TestString, N)) = False Then
            NewString = Left(TestString, N - 1) & " " & Right(TestString, Len(TestString) - N + 1)
            Exit For
        End If
    Next N
    End Sub
    Martin

  3. #3
    Forum Expert contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2013
    Posts
    1,430

    Re: Search & Replace InString

    Formula approach

    Function InsertGap(MyString As String) As String
        Dim Symbol As Variant
        Dim j As Long
        On Error Resume Next
        With WorksheetFunction
            For j = 1 To Len(MyString)
                Symbol = Symbol & Mid(MyString, j, 1)
                If Not IsNumeric(Mid(MyString, j, 1)) Then
                    Exit For
                End If
            Next j
        InsertGap = .Replace(MyString, Len(Symbol), 0, " ")
        End With
    End Function
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, ?Born in USSR?
    Vusal M Dadashev

    Baku, Azerbaijan

  4. #4
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Re: Search & Replace InString

    Thanks guys, but something is still not right.

    mrice, The code as is did not change anything. I've been trying to figure out what's not right with it. Yet to correct.

    Here's another attempt which does nothing.
    For N = 1 To Len(TestString)
        If IsNumeric(Left(TestString, N)) = True Then
           'TestString = Right(TestString,N)
            NewString = (Left(TestString, N))
             If InStr(1, NewString, NewString Like "[1-9],[A-Z]") > 0 Then
            ' If NewString Like "[1-9],[A-Z]" Then
           ' If INST("*",(NewString) = False Then
            TestString = Left(NewString, N - 1) & " " & Right(NewString, Len(NewString) - N) ' I Think something is not right with the right half of this line starting @ &Right(
            Exit For
        End If
        End If
        Next N
    Forum Guru, I'm not real sure how I would apply the formula to my code.
    If you can supply some code that I could use, then I'd be more than happy to try it.

    BDB
    Last edited by bdb1974; 04-30-2010 at 06:21 PM.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search & Replace InString

    Hello BDB,

    This is quite easy to do using Regular Expressions. Here is an example.
    Sub FindReplace()
    
      Dim RegExp As Object
      Dim Text As String
      
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Global = True
        RegExp.Pattern = "(\d+)(\w+)"
        
          Text = "1abc,12abc,123abc, 1234abc, 1234nabc"
          
          MsgBox RegExp.Replace(Text, "$1" & " " & "$2")
        
        Set RegExp = Nothing
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Re: Search & Replace InString

    Thanks Leith,

    I'm not sure what's happening.
    I got your script to work in a worksheet cell. But, when I attempt to place it into the userform where it's needed, I'm not getting any change/corrections to occur.

    Private Sub CommandButton2_Click()
      Dim Text As String
    
       Dim RegExp As Object
      With Txt_LOCATION   ' Name of text box
          'Text = "1abc,12abc,123abc, 1234abc, 1234nabc"
         Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Global = True
        RegExp.Pattern = "(\d+)(\w+)"
          Text = .Text
          Text = RegExp.Replace(Text, "$1" & " " & "$2")
    End With
           Set RegExp = Nothing
    :/

    BDB

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search & Replace InString

    Hello BDB,

    Do you want the modified text from the TextBox to be transfered to a worksheet cell or just displayed in the TextBox?

+ 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