VBA: How can I control a search&replace to only be valid for a defined sheet?

Rixn

Board Regular
Joined
Jun 4, 2005
Messages
119
Office Version
  1. 2021
Platform
  1. Windows
I import some text from a web page. The data is separated in rows and columns so it easily slip into cells in a nice row and column manner.

Now, I want to parse some names in one column (column A:A). The names do not reside in any other column. I want to change the content for those cells that is "Alpha Beta" into just "Beta", and I only want the replacing to be carried out on a specific sheet (named "Import").

I've recorded a VBA script and it look like this:

Cells.Replace What:="Alpha Beta", Replacement:="Beta", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

But, when I try different setting for the range I get the same script, I mean, for when I try the search & replace with settings for both the whole workbook and for the current sheet.

How can I control the search & replace with VBA to only be valid for a defined sheet?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I believe this would do it.

Code:
With Sheets("Import")
    .Cells.Replace What:="Alpha Beta", _
            Replacement:="Beta", _
                 LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
              MatchCase:=False, _
           SearchFormat:=False, _
          ReplaceFormat:=False
End With
 
Upvote 0
Rixn,

Sample raw data:


Excel 2007
AB
1Alpha Beta
2Alpha Beta
3Alpha Beta
4Alpha Beta
5
6
7
8Alpha Beta
9Alpha Beta
10
11
12
13Alpha Beta
14Alpha Beta
15
Import


After the macro:


Excel 2007
AB
1Beta
2Alpha Beta
3Beta
4Alpha Beta
5
6
7
8Beta
9Alpha Beta
10
11
12
13Beta
14Alpha Beta
15
Import


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub Test()

With Sheets("Import")

  .Columns("A:A").Replace What:="Alpha Beta", Replacement:="Beta", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End With

End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the Test macro.
 
Upvote 0
So, if I understand things correcly I can replace several phrases in the same column by doing this:

With Sheets("Import")
.Columns("A:A").Replace What:="Alpha Beta", Replacement:="Beta", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns("A:A").Replace What:="Delta Echo", Replacement:="Echo", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With

..and for different columns I can do this:

With Sheets("Import")
.Columns("A:A").Replace What:="Alpha Beta", Replacement:="Beta", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns("B:B").Replace What:="Delta Echo", Replacement:="Echo", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With

..or do I have to do this:

With Sheets("Import")
.Columns("A:A").Replace What:="Alpha Beta", Replacement:="Beta", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With

With Sheets("Import")
.Columns("B:B").Replace What:="Delta Echo", Replacement:="Echo", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With


Last question: How do I get the code tagged as code (I can't see any formatting tools even in the advanced mode)?
 
Upvote 0
..and for different columns I can do this:

With Sheets("Import")
.Columns("A:A").Replace What:="Alpha Beta", Replacement:="Beta", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns("B:B").Replace What:="Delta Echo", Replacement:="Echo", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With

Last question: How do I get the code tagged as code (I can't see any formatting tools even in the advanced mode)?

That should work. No need for double With statements.

See my signature for how to post code with code tags.
 
Upvote 0
Rixn,

When posting VBA code, please use Code Tags - like this:

[code]

'Paste your code here.

[/code]


This will work correctly:

Code:
With Sheets("Import")
  .Columns("A:A").Replace What:="Alpha Beta", Replacement:="Beta", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
  .Columns("B:B").Replace What:="Delta Echo", Replacement:="Echo", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,320
Members
448,887
Latest member
AirOliver

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