Excel VBA .Replace happens on specified sheet and others

Tanquen

New Member
Joined
Dec 14, 2017
Messages
39
Office Version
  1. 365
Platform
  1. Windows
This VBA code is working but sometimes the Find and Replace happens on the specified sheet "CSV" and other "Function" sheet with the same cell content. I'm guessing it because the "Function" sheet also has the range of cells with values to then find in the "CSV" sheet. But the .Replace function calls out the "CSV" sheet. ???

VBA Code:
Sub Button3_Click()
    '***Find & Replace - Link to Function Sheet Cells
        Dim sh As Worksheet, rng As Range, SFind As String, SReplace As String, fRng As Range, CL As Range
                    
        Set sh = Worksheets("CSV")
    
        With sh
            Set fRng = Worksheets("Function").Range("A10:A1000")
            
            Worksheets("Function").Range("B10:C1000").Replace What:="-", Replacement:="_"
            
            For Each CL In fRng
                'If Not CL Is Nothing Then
                If Not IsEmpty(CL.Value) Then
                    SFind = CL.Value
                    SReplace = "=Function!" & CL.Offset(, 1).Address
                    If Not IsEmpty(CL.Offset(, 1)) Then
                        Worksheets("CSV").Range("A1:D1000").Replace What:=SFind, Replacement:=SReplace, LookAt:=xlWhole
                    End If
                End If
            Next CL
        End With
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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