Feb 22 2024 03:15 AM
Hi all,
I've tried to play around with this as it should be fairly easy but I can't seem to change the cell D2 to a cell on a different sheet?
The other question I have is that if I am combining this macro with another, one do I need to include the bits in purple in the first line: Private Sub Worksheet_Change(ByVal Target As Range)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xCell As Range
If Target.Address = Range("D2").Address Then Exit Sub
Application.ScreenUpdating = False
For Each xCell In Range("R1:ZA1")
xCell.EntireColumn.Hidden = (xCell.Value < Target.Value)
Next
Application.ScreenUpdating = True
End Sub
Thanks
Feb 23 2024 12:29 AM
I'd be glad to help you with hiding columns based on a date on another tab in Excel. Here's a combined approach addressing the issues and incorporating improvements:
Understanding Your Requirements:
Solution:
Reference the Date Cell:
`Sheet2.Range("B2").Value`
Combine Macros:
Improved Macro:
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address <> "D2" Then Exit Sub 'Skip if not cell D2 Dim dateCell As Range Set dateCell = Sheet2.Range("B2") 'Reference date cell on Sheet2 Application.ScreenUpdating = False For Each xCell In Range("R1:ZA1") xCell.EntireColumn.Hidden = (xCell.Value < dateCell.Value) Next Application.ScreenUpdating = True End Sub
Explanation:
Additional Tips:
Remember:
I hope this comprehensive response helps you achieve your desired functionality!
Feb 23 2024 12:31 AM
Feb 26 2024 06:47 AM
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:
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.