deleting text in a string, then add new text to end of string

Mr_Mod

New Member
Joined
May 29, 2016
Messages
6
I have a list of text which all end in "Config File", each of these text strings vary in length. I wish to be able to delete the words "Config File" from each of these text strings, and after i have deleted the text then add the text "V2A" to each of the text strings.
Is it possible to do this in VBA, and if so how?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Assuming data is in Column A, starting at row 1, try this macro:
Code:
Sub ReplaceString()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("A1:A" & LastRow)
        rng = Replace(rng, "Config File", "V2A")
    Next rng
    Application.ScreenUpdating = True
End Sub

This formula should also do it: =SUBSTITUTE(A1,"Config File","V2A")
 
Last edited:
Upvote 0
Does this do what you want? (Assumed data in column A)
Test in a copy of your file.
Code:
Sub ReplaceConFigFile()
  Columns("A").Replace What:="Config File", Replacement:="V2A", LookAt:=xlPart, MatchCase:=False
End Sub
 
Upvote 0
Peter,

This worked great, i did however have to move data around so what I had to do was

text string to modify is in "sheet 1" cell "C5" and the modified text needs to appear in "sheet 2" cell "F3"
 
Upvote 0
.. i did however have to move data around ..

text string to modify is in "sheet 1" cell "C5" and the modified text needs to appear in "sheet 2" cell "F3"
Not too surprising that you had to "move data around" since you didn't originally tell us where the data was or where the results were to go. ;)

Given the additional information, I think this should do it without moving anything yourself.

Code:
Sub ReplaceConFigFile_v2()
  With Sheets("sheet 1").Range("C5", Sheets("sheet 1").Range("C" & Rows.Count).End(xlUp))
    Sheets("sheet 2").Range("F3").Resize(.Rows.Count).Value = .Value
    Sheets("sheet 2").Range("F3").Resize(.Rows.Count).Replace What:="Config File", Replacement:="V2A", LookAt:=xlPart, MatchCase:=False
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top