Vba replace a list of cells with blanks in entire workbook

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a list of data in column x in sheet1 data starts in X1 and goes down to x20

what I need is 1 by 1 search the values in X1 to x20 and try to find that value in the 1st column of every sheet and delete that value. If it can't find it don't do anything and if a cell in X1 to x20 is blank then exit the sub

thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Something like this to get you started

Code:
Sub forEachWs()
    Dim ws 		As Worksheet
	Dim r		as range
    
	
	For Each ws In ActiveWorkbook.Worksheets
	
	For each r in ws.Range("X1:X20")
		If r = 'First Column Value
			'Delete Value
		Else
	'Do nothing
		next r
	
	Next
	
End Sub
 
Upvote 0
.. try to find that value in the 1st column of every sheet ..
Some clarification is required.
If one of the values to look for is "cat"

Do we delete if a cell is found with "The cat sat on the mat"? If so, are we just deleting cat leaving "The sat on the mat" or delete the whole cell?

Do we delete if a cell is found with "Tom was scathing"? If so, are we just deleting cat leaving "Tom was shing" or delete the whole cell?

Your thread title mentions replacing but your description is about deleting. Can you clarify that issue too please?
 
Upvote 0
Apologies for not being clearer.

Will actually need to search for only cells that match and will also be a number i.e. 12345678 or 12357681 etc. Once found those figures can be deleted/cleared or replaced with "" from whichever sheet they are on including duplicates
 
Last edited:
Upvote 0
Give this a try in a copy of your workbook.

Code:
Sub Delete_Values()
  Dim ws As Worksheet
  Dim i As Long
  Dim vVal As Variant
  
  i = 1
  Do While Not IsEmpty(Sheets("Sheet1").Range("X" & i).Value)
    vVal = Sheets("Sheet1").Range("X" & i).Value
    For Each ws In Worksheets
      If ws.Name <> "Sheet1" Then ws.Columns("A").Replace What:=vVal, Replacement:="", LookAt:=xlWhole
    Next ws
    i = i + 1
  Loop
End Sub
 
Upvote 0
Thanks peter. Works perfectly. One quick thing if the data started in X5.
I tried range("x5" & I).value but it didn't work.
 
Upvote 0
Thanks again but the problem I have is sometimes people insert rows so the position moves. And code doesn't update to reflect those changes

Can it be changed so the 1st cell is a named cell. I.e. call X5 startcell
 
Upvote 0
Thanks again but the problem I have is sometimes people insert rows so the position moves. And code doesn't update to reflect those changes

Can it be changed so the 1st cell is a named cell. I.e. call X5 startcell
Yes, if we are only worried about rows being inserted or deleted then change that same line to
Code:
i = Range("startcell").Row
 
Last edited:
Upvote 0
Thanks peter. Just tried the same code on a different workbook which has some hidden sheets and something weird happened. The values were removed in the summary sheet and didn't remove them from the other sheets ???
 
Upvote 0

Forum statistics

Threads
1,224,852
Messages
6,181,403
Members
453,036
Latest member
Koyaanisqatsi

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