How to use replace correctly in this code?

F_Ribeiro

New Member
Joined
Jan 14, 2021
Messages
28
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello everybody!

I am doing an exercise that aims to change the value of a cell using the replace function. Depending on the value in a cell, I should change all the content in another cell, but I want to make this task more complete and the way I wrote it, it doesn't work.

Basically it is a table with five columns and column "E" contains numbers. Whenever there is "0000" in a row in that column, an eight character number in the same row as column "B" must be changed. The string which usually starts with "50", must start with "53". So I intend to replace only the first two characters of this string using the replace function in an If.

This is where my problem begins, because I wanted to make a reference to the cell I want to change in the formula, and not the string in it. Especially because if in any line that this condition is met the cell would already start with "53" and still have "50" in the string, that value would be replaced. I left my code commented below, I will be grateful if there is a solution and I am open to suggestions.



VBA Code:
Sub F_Ribeiro()
Dim W As Worksheet
Dim xlastRow As Long
Dim xRow As Long
   
Set W = Sheets("Sheet1")
xlastRow = Selection.End(xlDown).Row
For xRow= 2 To lastRow
If W.Range("E" & xRow).Value = "0000" Then
    W.Range("B" & xRow).Value = VBA.Replace(W.Range("B" & xRow).Value, "50", "53", 1, 2, vbTextCompare)
End If
Next
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,508
Office Version
  1. 365
Platform
  1. Windows
Why not just use the MID function, so you can specify changing the first two characters only, i.e.
VBA Code:
    W.Range("B" & xRow).Value = "53" & MID(W.Range("B" & xRow),3)
 
Solution

F_Ribeiro

New Member
Joined
Jan 14, 2021
Messages
28
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Why not just use the MID function, so you can specify changing the first two characters only, i.e.
VBA Code:
    W.Range("B" & xRow).Value = "53" & MID(W.Range("B" & xRow),3)
Who knew the solution could be that simple. Sure, I'm still a little frustrated that I don't know how to use Replace, but the important thing is that the code runs, KKKKKKKKK!

Thanks a lot!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,508
Office Version
  1. 365
Platform
  1. Windows
You are welcome

Here is a write-up on the REPLACE function: MS Excel: How to use the REPLACE Function (VBA)
Note that the issue is that the REPLACE function looks for the value ANYWHERE in your string. It is not limiting to the first two characters.
If you need to check the first two characters to see if the were "50" first before doing the replace, you would use the left function to do that, i.e.
VBA Code:
IF LEFT(W.Range("B" & xRow),2) = "50" THEN
 

Watch MrExcel Video

Forum statistics

Threads
1,132,646
Messages
5,654,561
Members
418,140
Latest member
ahepple86

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
Top