Replace Function Not Replacing Values Within A Range

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Code:
With ws_stsked
    dt_row = Application.WorksheetFunction.Match(CLng(Date), .Columns(1), 0)
    elim = Application.WorksheetFunction.VLookup(FullName, ws_rstr.Range("E:H"), 4, False)
    rplc = str4
    Set rng_scour = .Range("B" & dt_row & ":HA375")
    With rng_scour
        With .Cells
            .Replace What:=elim, Replacement:=rplc, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        End With
    End With
End With

The idea of this code is to replace all the values of 'elim' found with the value of 'rplc' within the range set to 'rng_scour' (B & dt_row : HA375) of worksheet alias ws_stsked.

I don't get any errors, but nothing is being replaced. I suspect my approach my be wrong with the use of .cells within the range.

Any suggestions?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
try (untested)

Code:
[COLOR=#ff0000]Dim Cel As Range[/COLOR]
With ws_stsked
  dt_row = Application.WorksheetFunction.Match(CLng(Date), .Columns(1), 0)
  elim = Application.WorksheetFunction.VLookup(FullName, ws_rstr.Range("E:H"), 4, False)
  rplc = str4
  Set rng_scour = .Range("B" & dt_row & ":HA375")

  [COLOR=#ff0000]For Each Cel In rng_scour[/COLOR]
       [COLOR=#ff0000]Cel = Cel.Replace[/COLOR](What:=elim, Replacement:=rplc, LookAt:=xlPart, MatchCase:=False)
  [COLOR=#ff0000]Next Cel
[/COLOR]
End With
 
Last edited:
Upvote 0
Your two internal with blocks are pointless, but the code itself looks fine. Are you sure the search value actually exists?

All you need is:

Code:
rng_scour.Replace What:=elim, Replacement:=rplc, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
 
Upvote 0
Thank you both for helping out.
It turned out that the worksheet in which the replacement process applied was protected. For anything to work, I had to unprotect the worksheet first.
I found it unusual I didn't get an error message.
 
Upvote 0
I continue to have a problem with this concept. Help?

Rich (BB code):
                With ws_stsked
                    .Unprotect
                    d3 = Application.WorksheetFunction.Match(CLng(Date), .Columns(1))
                    Set rng_d3 = .Range("B" & d3 & ":HA" & d3)
                    rng_d3.Replace What:=etx, Replacement:=etv, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
                    'For Each cel In rng_d3
                    '    cel = cel.Replace(What:=etx, Replacement:=etv, LookAt:=xlPart, MatchCase:=False)
                    'Next cel
                    cnt_etx = Application.WorksheetFunction.CountIf(.Cells, etx)
                    cnt_etv = Application.WorksheetFunction.CountIf(.Cells, etv)
                    MsgBox etv & " has been scheduled " & cnt_etv & " shifts."
                    .Protect
                End With

No errors, but the instances of 'etx' (WPStudent3) are not being replaced with the value etv (Student).
The value of "WPStudent3" exists in the worksheet, from row 93 (d3) to the end 106 times.

I also tried the routine in green as an alternative and I got the same results. The worksheet is unprotected.
 
Upvote 0
Excel 2016 (Windows) 32 bit
B
C
364
BEFORE
365
etx student1etx student101
366
etx student2etx student102
367
etx student3etx student103
368
etx student4etx student104
369
etx student5etx student105
370
etx student6etx student106
371
etx student7etx student107
372
etx student8etx student108
373
etx student9etx student109
374
etx student10etx student110
375
etx student11etx student111
Sheet: Sheet1

Excel 2016 (Windows) 32 bit
B
C
364
AFTER
365
etv student1etv student101
366
etv student2etv student102
367
etv student3etv student103
368
etv student4etv student104
369
etv student5etv student105
370
etv student6etv student106
371
etv student7etv student107
372
etv student8etv student108
373
etv student9etv student109
374
etv student10etv student110
375
etv student11etv student111
Sheet: Sheet1

Use @RoryA method - it is efficient
Code:
Sub ReplaceValues()

    Dim Cel As Range, ws_stsked As Worksheet, elim As String, rplc As String, dt_row As Long
    Set ws_stsked = Sheets(1)
    elim = "etx":   rplc = "etv":
    
    ws_stsked.Range("B365:HA375").Replace what:=elim, replacement:=rplc, MatchCase:=True        'case insensitive would be  MatchCase:=False

End Sub
 
Upvote 0
I have no reason not to trust these solutions, and I thank everyone for their suggestions, but I just am unable to get this to work.

Yongle, based on your suggestion ....

Code:
     With ws_stsked
                    .Unprotect
                    d3 = Application.WorksheetFunction.Match(CLng(Date), .Columns(1))
                    'Set rng_d3 = .Range("B" & d3 & ":HA" & d3)
                    ws_stsked.Range("B" & d3 & ":HA" & d3).Replace What:=etx, Replacement:=etv, MatchCase:=False
                    cnt_etx = Application.WorksheetFunction.CountIf(.Cells, etx)
                    cnt_etv = Application.WorksheetFunction.CountIf(.Cells, etv)
                    MsgBox etv & " has been scheduled " & cnt_etv & " shifts."
                    .Protect
                End With

.... I'm still not getting the replacements happening.

ws_stsked is recognized properly.
The sheet is unprotected.
etx = "WPStudent3" and it does exist a number of times in the range. I checked the length of that value in that worksheet to check to ensure there wasn't some invisible trailing space or anything, and their wasn't.
etv = "Martin"

I don't know what to do now if even you pros are stumped.
 
Upvote 0
and using your values

Code:
Sub ReplaceValues()

    Dim Cel As Range, ws_stsked As Worksheet, etx As String, etv As String
    Set ws_stsked = Sheets(1)
    etx = "[COLOR=#ff0000]WPStudent3[/COLOR]"
    etv = "[COLOR=#006400]Martin[/COLOR]"
    
    ws_stsked.Range("B365:HA375").Replace what:=[COLOR=#ff0000]etx[/COLOR], replacement:=[COLOR=#006400]etv[/COLOR], MatchCase:=True        'case insensitive would be  MatchCase:=False

End Sub


Excel 2016 (Windows) 32 bit
A
B
C
364
before
365
a string WPStudent3a string WPStudent3 a string
366
a string WPStudent4a string WPStudent3 a string
367
a string WPStudent5a string WPStudent3 a string
368
a string WPStudent6a string WPStudent3 a string
369
a string WPStudent7a string WPStudent3 a string
370
a string WPStudent8a string WPStudent3 a string
371
a string WPStudent9a string WPStudent3 a string
372
a string WPStudent10a string WPStudent3 a string
373
a string WPStudent11a string WPStudent3 a string
374
a string WPStudent12a string WPStudent3 a string
375
a string WPStudent13a string WPStudent3 a string
Sheet: Sheet1

Excel 2016 (Windows) 32 bit
A
B
C
364
after
365
a string Martina string Martin a string
366
a string WPStudent4a string Martin a string
367
a string WPStudent5a string Martin a string
368
a string WPStudent6a string Martin a string
369
a string WPStudent7a string Martin a string
370
a string WPStudent8a string Martin a string
371
a string WPStudent9a string Martin a string
372
a string WPStudent10a string Martin a string
373
a string WPStudent11a string Martin a string
374
a string WPStudent12a string Martin a string
375
a string WPStudent13a string Martin a string
Sheet: Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,558
Messages
6,125,507
Members
449,236
Latest member
Afua

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