Replace a word with nothing VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have the word 'Thomson's' in cell C2, but I want to replace it with nothing, using VBA.

The code below replaces it with a blank space instead of nothing - if you use =len(C2) in cell D2 after the code is run, it shows that there is ONE character instead of zero characters.

Does anyone know how to replace text with nothing using VBA, please?

VBA Code:
Sub ReplaceThomsons()


    Columns("C").Replace What:="Thomson's", _
                            Replacement:="", _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            MatchCase:=False, _
                            SearchFormat:=False, _
                            ReplaceFormat:=False
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
My guess is that your code is actually working correctly.
I am willing to bet that the issue is that there is an extra space either before or after the word "Thomson's" in your data.

So it is probably correctly replacing "Thomson's" with nothing, but it is not replacing that extra space that you probably have in there along with "Thomson's".
 
Upvote 0
How about
VBA Code:
Sub ReplaceThomsons()


    With Columns("C")
            .Replace What:="Thomson's", _
                            Replacement:="", _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            MatchCase:=False, _
                            SearchFormat:=False, _
                            ReplaceFormat:=False
            .Value = .Value
    End With
End Sub
 
Upvote 0
Yes, that was a good (and correct) guess - @Joe4 - thank you!

And @Fluff the code you wrote above worked as intended, so thank you!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,558
Latest member
aivin

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