Search large cell range and clear any cell(s) where value criteria met.

Wait what

New Member
Joined
Sep 21, 2023
Messages
2
Office Version
  1. 2013
  2. 2011
  3. 2010
Platform
  1. Windows
  2. Mobile
Hi, I am still getting starting this excel coding, and thought that I kind of had it down pat, but my macro is not doing anything :(
Any help/tips would be greatly appreciated!!

I have data in cells B2:AB324 and would like the macro to scan/loop through all cells in range, to identify and clear the cell contents
for any cells containing the value of -9.

I have more than one sheet in this workbook, hence the specification; and so far, have come up with this (completely ineffective) code:

Sub ClearNeg9s()

'Clears all cells in range containing '-9'

'Turn off screen updating, for User aesthetics
Application.ScreenUpdating = False

'Only run on 'Problem 2' page!
Sheets("Problem 2").Select

'Define Range of cells to scan
AllMaps = Range("B2:AB324").Value

'Set RowCounter starting position and start scanning
RowCntr = 2
Do Until Cells(RowCntr, 2).Value = "-9"

'Clear '-9' and leave cell empty
If Cells(RowCntr, 2).Value = "-9" Then
Cells(RowCntr, 2).ClearContents
End If

RowCntr = RowCntr + 1

Loop

Cells(RowCntr, 2).Value = AllMaps
Cells(RowCntr, 2).Value = RowCntr - 1

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the Board!

A loop is totally unnecessary here. Simply select your range, and do a Find and Replace, replacing -9 (no double-quotes!) with nothing.
If you turn on the Macro Recorder and record yourself doing this manually, it will give you all the VBA code you need to do this.
 
Upvote 0
Huh! I don't know if I've done a find and replace! I actually did Record a Macro earlier to see; tabbed over cell by cell to clear the specific ones. That code would have been ridiculously long had I continued through the whole range.
This is definitely worth a shot... Thanks!

(And Thanks for the welcome! :)
 
Upvote 0
You are welcome.

Feel free to post back if you run into any issues.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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