Link to home
Start Free TrialLog in
Avatar of Bright01
Bright01Flag for United States of America

asked on

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.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Why do you need to clear them?
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

Avatar of Bright01

ASKER

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.
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.
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

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Put it right under the Option Explicit you already have. If you still have the INDICATOR constant, put it INDICATOR2 in the same place.
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
Please attach your workbook.
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.