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.
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