Macro to clear certain range if a certain cell has a particular value

ardykav

Board Regular
Joined
Oct 18, 2015
Messages
172
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am looking to run a macro that clears a range of cells if one particular cell has the word "Leftout" in it. I have the below code but not sure how to run it properly as when i prompt to run it asks for a macro name, obviously I am missing something here.
So when Cell F20 has the word leftout in it i want to run the macro so it will then clear cells C19 to C29


Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F20")) Is Nothing Then
If UCase(Range("F20")) = "Leftout" Then
Application.EnableEvents = False
Range("C19:C29").ClearContents
Application.EnableEvents = True
End If
End If
End Sub

thanks in advance
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I think it should be:
Code:
If UCase(Range("F20")) =[COLOR=#000080] "LEFTOUT"[/COLOR] Then
You can't run it manually.
It is an event driven Sub, so it will run automatically every time you change cell F20 value.
 
Upvote 0
Ok so I more need it for the end of each week to clear a document, I dont want it to run everytime the cell in question is populated. Is there any way I can attach a button to this so it works whenever I press it?

thanks
 
Upvote 0
Ok so I more need it for the end of each week to clear a document, I dont want it to run everytime the cell in question is populated. Is there any way I can attach a button to this so it works whenever I press it?

thanks

Ok
1. In VBE create/insert a module
2. Put the macro 'toClear' in the module
3. create a button in the sheet in question
4. assign the macro to the button

Code:
Sub toClear()
If UCase(Range("F20")) = "LEFTOUT" Then
Range("C19:C29").ClearContents
End If
End Sub
 
Last edited:
Upvote 0
thanks, i played around for it a bit and looks like this is the simplest way and it works, thanks for all of your assistance

Sub Clearcontents()

If Range("F20") = "Leftout" Then
Range("C19:C19").Clearcontents

End If
End Sub
 
Upvote 0
thanks, i played around for it a bit and looks like this is the simplest way and it works, thanks for all of your assistance

Sub Clearcontents()

If Range("F20") = "Leftout" Then
Range("C19:C19").Clearcontents

End If
End Sub

You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,930
Members
452,367
Latest member
TePunaBloke

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