Macro written but Columns wont auto unhide but rows will,

mistatasty

New Member
Joined
Jul 21, 2021
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I need to hide certain columns based on certain cells being 0 as well as hide certain rows when certain cells are 0. I had a previous code to auto hide/unhide the rows based on the reference cell changing from 0. It worked fine and when the 0 changed the row would automatically unhide. However, I tried to input the same sort of macro to hide columns, but now the columns wont unhide automatically when the reference cell changes from 0, it has to be done manually. What is wrong here?

Private Sub Worksheet_Calculate()
Dim c As Range

Application.EnableEvents = False

For Each c In Range("A26:A247")

If c.Value = "0" Then
Rows(c.Row & ":" & c.Row).EntireRow.Hidden = True
Else
Range(c.Row & ":" & c.Row).EntireRow.Hidden = False

End If
Next
Application.EnableEvents = True
End Sub

Sub Hide_Columns_Containing_Value()
Dim c As Range
Application.EnableEvents = False

For Each c In Range("e25:an25").Cells
If c.Value = "0" Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False
End If
Next
Application.EnableEvents = True

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
VBA code placed in "Worksheet_Calculate" event procedures run automatically upon a calculation happening in the sheet.
(Event Procedure VBA code is VBA code that runs automatically upon some event happening - they must be placed in the correct place and must be named a certain way).

Your second procedure (the one that hides the columns) is not event procedure VBA code, so it will not run automatically.
You would need to place this code in the "Worksheet_Calculate" event procedure also, under the other code.
 
Upvote 0
VBA code placed in "Worksheet_Calculate" event procedures run automatically upon a calculation happening in the sheet.
(Event Procedure VBA code is VBA code that runs automatically upon some event happening - they must be placed in the correct place and must be named a certain way).

Your second procedure (the one that hides the columns) is not event procedure VBA code, so it will not run automatically.
You would need to place this code in the "Worksheet_Calculate" event procedure also, under the other code.
haha figured it out a minute before you replied. thank you though
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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