Forum Discussion
Hiding Columns based on date on on another tab
To ease our communications, give us the name of whatever this worksheet is and also the name of the "different sheet". In the absence of such info, I will call them Sheet1 and Sheet2, respectively.
It appears that you have a misunderstanding of certain things:
- Worksheet_Change is not a macro; it is an event handler. (Both are procedures that contain VBA code.) An event handler is code that is executed automatically by Excel when its event type (e.g., cell content change) occurs for its object (e.g., a specific worksheet). In this case that is when cell content is changed for one or more cells on Sheet1. So you will not be "combining this macro with another". More on this below. (There can be zero or one worksheet event handler per worksheet per event type; each worksheet can have its own event handler for an event type, containing either identical or different code. When you make a copy of a worksheet, you get a copy of its event handlers.)
- Simply put, yes, the Worksheet_Change event handler definition must be followed by "(ByVal Target As Range)". Target represents which cell or cells have been modified, and that's the mechanism by which VBA code can take action specific to the changed cells. (OK, technically, you can change the parameter name Target to a different name, e.g., to CellsThatChanged, but that may be wasted effort, as there is no functional difference.)
- The scope of the Worksheet_Change event handler can be changed from Private to Public, but any code you write to invoke it would have to provide the required parameter, and it would probably lead to insane coding.
- Each Worksheet object (it is implicitly "Me" in worksheet event handlers, referring to the Worksheet whose code module contains the event handler) has a Range property that this code is referencing (so explicitly, it's "Me.Range(…"). So yes, "D2" is a cell on Sheet1. And Target represents only a cell (or set of cells) on Sheet1.
It does not make sense that you want the address of cell D2 on another worksheet. If you want the value from that cell, you can use a reference such as Sheets("Sheet2").Range("D2").Value. Performance note: If you are going to use that retrieved value repeatedly, capture it into a local variable (defined As Variant, if you don't have confidence the value will be a more-specific type), and use the variable in subsequent code.
OTOH, if you are trying to ignore changes to cell D2 on Sheet1, the syntax might be
If Target.Address = "$D$2" Then Exit Sub
'or
If Target.Address(False, False) = "D2" Then Exit Sub
The problem with that code is... (Remember from above: The event handler is executed when one or more cells are modified.) The user can (for example) modify content in both cells C2 and D2 with a single action, in which case Target.Address will be $C$2 (not $D$2). Code can be written to skip processing of changes to D2 regardless, but I won't go into that without clarification of what you intend your statement to accomplish.
Regarding "combining" code, you may instead need a custom, separate procedure of Public scope that can be called from each Worksheet_Change event handler. Such procedure would go into a (standard) code module, the same type of code module where macros are stored. But again, I won't go into details without clarification of what you intend your code to accomplish.