VBA will not run until a cell is selected

si_1970

New Member
Joined
Aug 26, 2013
Messages
13
Hello All

I have a workbook calendar where I am using the below macro entered in the February sheet to hide the 29th Feb date column (AD) based on the value in cell A5. The A5 cell value is controlled by a formula (Cell A5 value of 1 = hide the column & Cell A5 value of "Leap Year" = show the column).

Everything works as it should however the macro will not run until I select a cell on the February worksheet. Is there a way to make the macro run as soon as the A5 value changes?

Thanks in advance.

Private Sub Worksheet_selectionchange(ByVal Target As Range)
If Sheets("February").Range("A5").Value = 1 Then
Sheets("February").Columns("AD").EntireColumn.Hidden = True
Else
Sheets("February").Columns("AD").EntireColumn.Hidden = False
End If


End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Need to check in the Change event of the worksheet. Not selection change

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    'check if user has changed Cell A5
    If Target.Address = Range("A5").Address Then
        If Sheets("February").Range("A5").Value = 1 Then
            Sheets("February").Columns("AD").EntireColumn.Hidden = True
        Else
            Sheets("February").Columns("AD").EntireColumn.Hidden = False
        End If
    End If
End Sub
 
Upvote 0
I have got it to work now. Strange I could have sworn I tried the below before and it did not work.

Private Sub Worksheet_Calculate()
Application.EnableEvents = False
If [A5].Value = "1" Then
Sheets("February").Columns("AD").EntireColumn.Hidden = True
Else
Sheets("February").Columns("AD").EntireColumn.Hidden = False
End If
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top