Clearing a set of Ranges in Excel

EE Pros,

I have 7 Range Names that currently have the letter P in them representing Checkmarks (FONT: Wingdings2).  I want to clear them with a Macro.  Something like this:

Sub ClearCheckboxes()
MACRO COMMAND, Range("RangeName2"), Range("RangeName3"), Range("RangeName4"), Range("RangeName5"), Range("RangeName6"), Range("RangeName7"))
            If .Value = "P"
               Then .Value = ""
End If
End Sub

Can I get some help writing what should be a rather simple Macro?

Thank you in advance,

B.
Bright01Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Why do you need to clear them?
Martin LissOlder than dirtCommented:
The following assumes you already have
Public Const INDICATOR = "RangeName"

Sub ClearNR()
Dim nrUseCase As Name
Dim nrRange As Range
Dim rngRow As Range
Dim intPos As Integer
 
For Each nrUseCase In ThisWorkbook.Names
    If Left$(nrUseCase.Name, Len(INDICATOR)) = INDICATOR Then
        intPos = InStr(1, nrUseCase.RefersTo, "!")
        Set nrRange = Range(Mid(nrUseCase.RefersTo, intPos + 1))
        For Each rngRow In nrRange.Rows
            rngRow.Columns(1) = ""
        Next
    End If
Next
End Sub

Open in new window

Bright01Author Commented:
Martin,

When the model is first brought up, it should have no selections already made.  When someone sits down with a customer, they then get concurrence which then they check the boxes.  That's why I need a reset capability.

In the example above, I provided the wrong Range name list.  I created a set of range names that aree only over the cells in column A and only in the unprotected cells where check marks occur.  The names were "Checkrange1", "Checkrange2", "Checkrange3", etc.  Is the code above specific to clearing these ranges?  I wouldn't want to get confused with other range names in the list.

B.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Martin LissOlder than dirtCommented:
The above code will set all cells in column A of only the ranges that start with "RangeName" to blank. I assume that that is the same as setting just to "P" cells to blank because I assume that the cells can only be "P" or blank.
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try something like this if you just want to clear the cells contents in those named ranges...

Sub ClearCells()
Dim rng As Range
Set rng = Union(Range("Checkrange1"), Range("Checkrange2"), Range("Checkrange3"))
rng.Cells.ClearContents
End Sub

Open in new window

Danny ChildIT ManagerCommented:
if you hit F5 (for the Find shortcut), and then select your Range name, could you not use Find/Replace (CTRL + H) and replace P with Q (ie Tick with Cross)

or, Replace P with "" (nothing....)

Note, you can have one "master" range that includes multiple, separated areas of cells.
Bright01Author Commented:
Martin,

How would I change the code to reflect the Check Box Names (CheckRange1, CheckRange2, etc.)?

Sktneer,

I tried your code and it worked for the first 3 Ranges.  When I added 4 more (total of 7), I got a Error 400.  Any thoughts?

DanCh99,

Thanks for the approach but I'm building this for people who don't know how to use Excel.

B.
Martin LissOlder than dirtCommented:
Option Explicit
Public Const INDICATOR2 = "CheckRange"

Sub ClearNR()
Dim nrUseCase As Name
Dim nrRange As Range
Dim rngRow As Range
Dim intPos As Integer
 
For Each nrUseCase In ThisWorkbook.Names
    If Left$(nrUseCase.Name, Len(INDICATOR2)) = INDICATOR2 Then
        intPos = InStr(1, nrUseCase.RefersTo, "!")
        Set nrRange = Range(Mid(nrUseCase.RefersTo, intPos + 1))
        For Each rngRow In nrRange.Rows
            rngRow.Columns(1) = ""
        Next
    End If
Next
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Try this to see if that works for you.....

Sub ClearCells()
Dim rng As Name
For Each rng In ActiveWorkbook.Names
   Range(rng).Columns(1).Cells.ClearContents
Next rng
End Sub

Open in new window

Bright01Author Commented:
Martin,

Trying now.


Sktneer,

Won't this cause a clearing of all the Range Names in the WS?

Sub ClearCells()
Dim rng As Name
For Each rng In ActiveWorkbook.Names
   Range(rng).Columns(1).Cells.ClearContents
Next rng
End Sub
Bright01Author Commented:
Martin,

Where do I insert the

Option Explicit
Public Const INDICATOR2 = "CheckRange"

I already have Option Explicit at the top of the WS.  Not sure where I put in the Public Const.

B.
Martin LissOlder than dirtCommented:
Put it right under the Option Explicit you already have. If you still have the INDICATOR constant, put it INDICATOR2 in the same place.
Bright01Author Commented:
OK.  One issue was I was trying to put this in the WS and you had it in Module1.  Yes... I have the INDICATOR constant and I've added INDICATOR2 with the code in the module.

I've added the button and connected to the ClearNR Sub.  But when I fire it, nothing.

Sub ClearNR()
'Clear Named Ranges (NR) for the check boxes (INDICATOR2)
Dim nrUseCase As Name
Dim nrRange As Range
Dim rngRow As Range
Dim intPos As Integer
 
For Each nrUseCase In ThisWorkbook.Names
    If Left$(nrUseCase.Name, Len(INDICATOR2)) = INDICATOR2 Then
        intPos = InStr(1, nrUseCase.RefersTo, "!")
        Set nrRange = Range(Mid(nrUseCase.RefersTo, intPos + 1))
        For Each rngRow In nrRange.Rows
            rngRow.Columns(1) = ""
        Next
    End If
Next
End Sub
Martin LissOlder than dirtCommented:
Please attach your workbook.
Bright01Author Commented:
Thanks guys!  Martin, the problem was I had the text as "RangeName" and you had it as Rangename".  Very case sensitive!  Changed it and it works.

Much thanks!

B.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.