VBA for Clearing Cell if value not stated in the array

NachoBoacity

New Member
Joined
Oct 18, 2022
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
I have column "D" with a lot of values, however I would only like to keep the cells within that column which contain values "132" or "133" or "54". How could I clear content if value is not stated in the array?

This is the closest code I found... It is broken as I tried to adjust it to my scenario, so probably not even worth looking at!

Thank you.


VBA Code:
Sub MyClearMacro()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row in column P with data
    lr = Cells(Rows.Count, "D").End(xlUp).Row
   
'   Loop through all rows
    For r = 2 To lr
        If Cells(r, "D") <> "133" Or "132"  OR "Etc Etc" Then Range(Cells(r, "D"), Cells(r, "D")).ClearContents
    Next r
   
    Application.ScreenUpdating = True
   
 
End Sub
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added the tags for you this time. 😊

See if this does what you want.

VBA Code:
Sub MyClearMacro()
    Dim lr As Long
    Dim r As Long
  
    Application.ScreenUpdating = False
'   Find last row in column P with data
    lr = Cells(Rows.Count, "D").End(xlUp).Row
  
'   Loop through all rows
    For r = 2 To lr
        Select Case Cells(r, "D").Value
          Case 133, 132, 54
          Case Else
            Cells(r, "D").ClearContents
        End Select
    Next r
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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