VBA replace same text in cells/table with text in first cell of each row

CaptnAbraham

New Member
Joined
Feb 10, 2022
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Greetings dear Excel specialists...

Using two VBA codes I was able to create a rather large-ish construct which has a) the filenames of a certain folder in column A (starting at A6 through A238) and b) copy the linked cells from the first row (B6:SL6) into the following rows of 7 to 238. A rough example is attached as a picture.

As a third step, which I can't figure out, I would like to replace the filename in the copied cells in each row with the value in column A of the same row.
B7:SL7 should change Filename001.xlsx into Filename002.xlsx which is in A7.
B8:SL8 should change Filename001.xlsx into Filename003.xlsx which is in A8.
B9:SL9 should change Filename001.xlsx into Filename004.xlsx which is in A9.
And so forth until last filled in cell in column A.

I have tried to look for a solution here and elsewhere on the internet but couldn't find a fitting solution. It would be much appreciated if someone could be of help.

Thanks for all the help.

CaptnAbraham
 

Attachments

  • VBA Excel Replace.PNG
    VBA Excel Replace.PNG
    45.9 KB · Views: 15

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this:
VBA Code:
Sub MyReplace()

    Dim lr As Long
    Dim r As Long
    Dim fnd As String
    Dim rep As String
    
'   Set value to find (from cell A6)
    fnd = Range("A6").Value
    
    Application.ScreenUpdating = True
    
'   Loop through rows 7 to 238
    For r = 7 To 238
'       Check for a value in column A
        If Cells(r, "A") <> "" Then
'           Get the value to replace
            rep = Cells(r, "A")
'           Do find/replace for columns B:SL
            Range(Cells(r, "B"), Cells(r, "SL")).Replace what:=fnd, replacement:=rep
        End If
    Next r
    
    Application.ScreenUpdating = False
    
End Sub
 
Upvote 0
Solution
That is indeed the solution. Works like a charm!

Having this as a basis, and indeed it being simple to understand, I'll be able to use it even far into the future.

Very much appreciated, Joe4!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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