VBA HELP - Replace (Not Equal)

wakerider017

Board Regular
Joined
Jun 10, 2015
Messages
77
I have the below code which seems to work well at replacing all in D7:D502 with #N/A that matches G1...

BUT, I want to replace all in D7:D502 with #N/A that does NOT match G1. How do I do that??



Code:
Range("D7:D502").Replace Range("G1"), "#N/A", xlWhole
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I'm not well versed in the replace function so I don't know if there is a way to use it how you want.

However you can loop through your range with an if statement:

Code:
Dim cell As Range

For Each cell In Range("D7:D502")
     If cell.Value <> Range("G1").value Then cell.Value = "#N/A"
Next cell
 
Last edited:
Upvote 0
Without a loop, perhaps:

Code:
Sub test2()
  Dim Addr As String
  Addr = "D7:D502"
  Range(Addr) = Evaluate(Replace("IF(@<>G1,""#N/A"",g1)", "@", Addr))
End Sub
 
Last edited:
Upvote 0
Sorry for the late reply.

I ended up using the code found here: https://www.thespreadsheetguru.com/...ells-not-containing-a-specific-word-or-phrase

Code:
[COLOR=#00007F][FONT='inherit']Sub[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] DoNotContainClearCells()[/FONT][/COLOR]
[COLOR=#007F00][FONT='inherit']'PURPOSE: Clear out all cells that do not contain a specific word/phrase[/FONT][/COLOR]
[COLOR=#007F00][FONT='inherit']'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault[/FONT][/COLOR]

[COLOR=#00007F][FONT='inherit']Dim[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] rng [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']As[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] Range[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']Dim[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] cell [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']As[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] Range[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']Dim[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] ContainWord [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']As[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']String[/FONT][/COLOR]

[COLOR=#007F00][FONT='inherit']'What range do you want to search?[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']Set[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] rng = Range("A1:A10")[/FONT][/COLOR]

[COLOR=#007F00][FONT='inherit']'What phrase do you want to test for?[/FONT][/COLOR]
[COLOR=#3D3D3D][FONT='inherit']  ContainWord = "Magic"[/FONT][/COLOR]

[COLOR=#007F00][FONT='inherit']'Loop through each cell in range and test cell contents[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']For[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Each[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] cell [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']In[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] rng.Cells[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']If[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] cell.Find(ContainWord) [/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Is[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Nothing[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Then[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] cell.Clear[/FONT][/COLOR]
[COLOR=#00007F][FONT='inherit']Next[/FONT][/COLOR][COLOR=#3D3D3D][FONT='inherit'] cell[/FONT][/COLOR]

[COLOR=#00007F][FONT='inherit']End[/FONT][/COLOR][COLOR=#00007F][FONT='inherit']Sub[/FONT][/COLOR]


And replaced cell.Clear with cell.Value = "#N/A"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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