Hide Column When Checkbox (Forms Control) is checked

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,539
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

The checkbox (Forms Control) is linked with cell D2 which returns True when the checkbox is checked and False when its unchecked


1582797594181.png


Then I am using the below code to hide/unhide rows

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
If Range("D2").Value = True Then Rows("1:1").EntireRow.Hidden = True
If Range("D2").Value = False Then Rows("1:1").EntireRow.Hidden = False


End Sub

The problem is every time I need to edit any cell in the worksheet for the event to trigger.
Is there any possibility to hide or unhide rows as soon as the checkbox is checked or unchecked.

I also tried the selection change event but for that too I need to click on a cell for the event to trigger.

Any help would be appreciated.

Regards,

Humayun
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hello All,

The checkbox (Forms Control) is linked with cell D2 which returns True when the checkbox is checked and False when its unchecked


View attachment 7740

Then I am using the below code to hide/unhide rows

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
If Range("D2").Value = True Then Rows("1:1").EntireRow.Hidden = True
If Range("D2").Value = False Then Rows("1:1").EntireRow.Hidden = False


End Sub

The problem is every time I need to edit any cell in the worksheet for the event to trigger.
Is there any possibility to hide or unhide rows as soon as the checkbox is checked or unchecked.

I also tried the selection change event but for that too I need to click on a cell for the event to trigger.

Any help would be appreciated.

Regards,

Humayun
try add this line in your code
VBA Code:
Application.EnableEvents = True
 
Upvote 0
Hi Abdelfattah,

Thanks for the reply...

I added the line you suggested..... but nothing is happening

I still have to edit a cell for the event to trigger :(

Any idea ??
 
Upvote 0
How about assigning this macro to the checkbox
VBA Code:
Sub hrayani()
   Dim Shp As Shape
   
   Set Shp = ActiveSheet.Shapes(Application.Caller)
   Rows(1).Hidden = Shp.ControlFormat.Value = 1
End Sub
 
Upvote 0
Hi Fluff,

Thanks as always for you help.... your solution is working great

Can you please tell me that is it also possible to call a macro when the checkbox is checked
 
Upvote 0
Just add
VBA Code:
Call Macro1
to the code & change macro1 to the name of your macro
 
Upvote 0
Hi Abdelfattah,

Thanks for the reply...

I added the line you suggested..... but nothing is happening

I still have to edit a cell for the event to trigger :(

Any idea ??
you know buddy when i use your code also it doesn't work me too but when i add this line
VBA Code:
Application.EnableEvents = True
it really works it is strange doesn't work with you but i 'm asking myself why doesn't work the code from the beginning it usually any code works when put in event change worksheet it is the mystery i hope find the answered from the experts
 
Upvote 0
The change event does not get triggered if a cell value changes as the result of a checkbox being (un)selected.
 
Upvote 0
Just add
VBA Code:
Call Macro1
to the code & change macro1 to the name of your macro
Hi Fluff,

I will try this and will get back to you Tomorrow. Not sure in which line to add this “Call Macro 1” though
 
Upvote 0
It doesn't matter where you add it :)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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