2

i cant manage to properly delete a text in multiple cells in a column in Excel. The sheet looks like this:

123 456 7|
123 456 7|
123 456 7|

I just want to get rid of 123 456. (By the way, 123 456 7 are all in one cell)

I think there's an error in my code:

Sub RemoveText()

Dim cell As Range

For Each cell In Range("A1:A3")
    cell.Value = Replace(cell.Text, "123 456", "", Count:=2)
Next

End Sub
trill
  • 148
  • 2
  • 9

2 Answers2

2

You need to use cell.Value also within the replace function

Sub test()

Dim rng As Range
Set rng = Range("A1:A3")

For Each cell In rng
    cell.Value = Replace(cell.Value, "123 456", "")
Next cell

End Sub
FloLie
  • 1,820
  • 1
  • 7
  • 19
2

No need for a loop or .value vs. .text. Simple one liner will do:

Range("A1:A3").Replace "123 456","",xlPart

Be mindful that the replace function can tie up resources when used on formula heavy and or data intensive ranges.

Scott Holtzman
  • 27,099
  • 5
  • 37
  • 72