FullColumn.Hidden in Worksheet_Change ()
Using Excel 2010, I edit an existing unprotected book and created in the teams event Worksheet_Change()
event EntireColumn.Hidden
, and EntireRow.Hidden
when the data validation cell, but they do not work.
Private Sub Worksheet_Change(ByVal Target As Range)
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
If Not Intersect(Target, Range("$C$2")) Is Nothing Then
Select Case Target.Value
Case "NO"
MsgBox "You just changed to HIDE" '<= Proves it fires
Range("$C$3").Value = "Invisible" '<= Does change cell
Columns("N:O").EntireColumn.Hidden = True '<= Doesn't hide
Case "YES"
MsgBox "You just changed to UNHIDE" '<= Proves it fires
Range("$C$3").Value = "Visible" '<= Does change cell
Columns("N:O").EntireColumn.Hidden = False '<= Doesn't unhide
End Select
End If
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Event fires since I have MsgBox
to prove it. I can change the values of the cells, etc., but not hiding / showing the column / row.
I copied my code into a new book and it works. So I copied it back to the original book, but as a fresh blank slate and it works. This still doesn't work in the original, significant sheet.
When I copied this into a simple macro it works as needed, hiding the correct columns, but when the button is clicked:
Sub HideThem()
Columns("N:O").EntireColumn.Hidden = True '<= DOES work
End Sub
I need this to automatically update based on the value of an individual cell. I even tried to call this mini Sub
from the event Worksheet_Change()
but it didn't work either.
Are there any known conflicts with other commands / events, inline buttons, images, merged cells, etc. that might prevent the columns / rows from being hidden?
I tried using a CheckBox
data validator instead of a cell YES/NO
to run the code (as that might be acceptable), but when I try to insert the ActiveX CheckBox
it says it can't insert the object, even in a completely new blank workbook. Could this be a related issue?
source to share
Suppose you have a drop-down list in cell C3 with two items: Visible and Invisible. The following code will hide the N and O columns when you change the C3 range value from empty / "visible" to "invisible". Before this action, you will need to read the message and click "OK". Changing from "Invisible" to "Visible" will present you with a message box. Click OK and you will see the hidden columns expand.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("C3") = "Invisible" Then
MsgBox ("You just changed to HIDE")
Columns(14).Hidden = True
Columns(15).Hidden = True
Else
If Range("C3") = "Visible" Then
MsgBox ("You just changed to UNHIDE")
Columns(14).Hidden = False
Columns(15).Hidden = False
End If
End If
End Sub
source to share