Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to zero out non-contiguous ranges
Dean wrote:
<<I am a little confused why you make it a public sub, as well as what you mean by qualifier. Does your approach mean that the macro would be seen by any EXCEL file, and so would save me time if I wanted to do a similar thing in another file? Normally, you start up a macro by using a CommandBar button (in this case named "Clear Named Range" or similar). If the routine were labeled Private, it would not show up in any list box so that you could either assign it to a CommandBar button or start it from the Tools|Macro|Macros dialog box. In general, macros should be written so that they can be used with whatever the currently active workbook is. Dean wrote: <<I will only use it on one worksheet (call it "output", which I will first pre-select within the macro) and only within this one workbook. I just want to put it in an existing garden variety module, I think. Can you rewrite your set command with this more simply-specified range? Assuming that your named range is global to the entire workbook (they usually are, unless you declared it like "output!MyNamedRange"), then the code below can be put in a standard code module, and will work regardless of what worksheet is active at the time the macro is called (the beauty of named ranges!). Notice the change of ActiveWorkbook to ThisWorkbook. This part "qualifies" the next part of the statement, which is the ..Names("MyNamedRange") part. If you had 2 workbooks of this same type open, the macro would not know which workbook you wanted to clear the cells on. This is why it is good programming practice to always clearly specify exactly where you want the macro to operate. ActiveWorkbook means the workbook that is currently active (the one you see on top of all other open workbooks). ThisWorkbook means the workbook that the macro is located in (which might be a workbook in the background). Public Sub ClearMyNamedRange() Dim rngCellsToClear As Range Set rngCellsToClear = ThisWorkbook _ .Names("MyNamedRange") _ .RefersToRange rngCellsToClear.Formula = 0 End Sub Dean wrote: <<Lastly, did you forget the part that would skip over any cells in the, changed-to-one-contiguous block that have protection in their formats, whether or not the sheet is protected? The macro above only changes any cells that are in the named range, so it will automatically skip any cells that are not specifically named in the Defined Name. There should be no need to worry about unprotecting and then reprotecting the worksheet, unless you have some of the cells in the named range protected. Normally, you don't protect cells where the user is supposed to make changes. You only protect cells that contain formulas, because you don't want the logic of the worksheet to be changed. Hope these explanations help. -- Regards, Bill Renaud |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How to zero out non-contiguous ranges | Excel Programming | |||
Non contiguous ranges | Excel Programming | |||
vlookup on non-contiguous ranges | Excel Worksheet Functions | |||
Adding Non-Contiguous Ranges | Excel Discussion (Misc queries) | |||
sorting non contiguous ranges | Excel Discussion (Misc queries) |