+ Reply to Thread
Results 1 to 4 of 4

Replacing the second instance of a string within another string.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Replacing the second instance of a string within another string.

    I am trying to search though a column of data and replace all second instances of a specific string within a larger string. If the second instance isn't there then I don't perform a replacement. What I am finding is the replacement replaces everything in front of the string including the string. Obviously I am missing something with the syntax. Can someone look at the code and inform me what I am missing? Thank you in advance!

    Sub test()
        Dim x As Long
        Dim last_R As Long
        
        last_R = Range("A65536").End(xlUp).Row
        
        For x = 1 To last_R
            my_var1 = InStr(1, Range("A" & x).Value, " xxx", vbTextCompare)
            my_var2 = InStr(my_var1 + 4, Range("A" & x).Value, " xxx", vbTextCompare)
            
            If my_var2 = 0 Then
                'do nothing
            Else
                my_var = Mid(Range("A" & x).Value, my_var2, 4)
                
                Range("A" & x).Value = Replace(Range("A" & x).Value, my_var, "", my_var2, 1, vbTextCompare)
            End If
        Next x
    End Sub
    The example workbook lists 5 possible situations that might come up. Only lines 1 and 2 should be changed from what should happen.
    Attached Files Attached Files
    Last edited by 111StepsAhead; 08-13-2013 at 09:45 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Replacing the second instance of a string within another string.

    The Replace doesn't look right.

    Try this instead.
                my_var = Left(Range("A" & x), my_var2 - 1) & Mid(Range("A" & x), my_var2 + 4)
                
                Range("A" & x).Value = my_var
    If posting code please use code tags, see here.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Replacing the second instance of a string within another string.

    For x = 1 to Last_R
        With Range("A" & x)
            .Value = WorksheetFunction.Substitute(CStr(.Value), " xxx", vbNullString, 2)
        End Wiht
    Next x
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Replacing the second instance of a string within another string.

    Norie, your code worked. It's very strange that Replace() isn't working today though. I've used it successfully in many other instances.

    http://www.techonthenet.com/excel/fo...eplace_vba.php

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. nth instance of a character in a string
    By AndrewMac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-27-2013, 11:20 AM
  2. Find nth instance of a character in a string
    By BRISBANEBOB in forum Excel General
    Replies: 1
    Last Post: 12-26-2011, 07:09 PM
  3. Finding the second instance of a value after a text string
    By ryantaylor in forum Excel General
    Replies: 9
    Last Post: 03-02-2011, 11:18 AM
  4. Replacing Misspelled sub-string in varying full string
    By freerdj in forum Excel General
    Replies: 15
    Last Post: 08-28-2009, 08:00 PM
  5. The first instance of a number in a string
    By Tracer123 in forum Excel General
    Replies: 2
    Last Post: 05-21-2008, 11:39 AM

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