Results 1 to 4 of 4

Replacing the second instance of a string within another string.

Threaded 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.

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