Hide Columns on multiple sheets based on option button

iSoleil

New Member
Joined
Oct 14, 2017
Messages
17
Hi guys,

My code to show/hide columns on multiple tabs does seem to work. I know it's very basic, but I just learned VBA codes from googling it and I can't seem to crack this one.

What I want to do is hide columns F:J on sheets 1-31 (same sheet names too) if OptionButton1 is clicked and show columns F:J if OptionButton2 is clicked.

This was my very mediocre attempt at testing it on Sheet1. Surprise, surprise. Not working.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If OptionButton1.Value = True Then
    Sheet1.Columns("F:G").Hidden = True
Else
    Sheet1.Columns("F:G").Hidden = False
End If
End Sub


Any help is greatly appreciated. Thanks!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Maybe, this untested code

Code:
Option Explicit


Sub Worksheet_Change(ByVal Target As Range)
Dim sh As Worksheet
For Each sh In Worksheets
If Sheets("Sheet1").OptionButton1.Value = True Then
    sh.Shapes.Columns("F:G").Hidden = True
Else
    sh.Shapes.Columns("F:G").Hidden = False
End If
Next sh
End Sub
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.OptionButton1.Value = True Then
For i = 1 To 31
    Sheets(i).Range("F1:J1").EntireColumn.Hidden = True
Next
Else
For i = 1 To 31
    Sheets(i).Range("F1:J1").EntireColumn.Hidden = False
Next
End If
ActiveSheet.OptionButton1.Value = False
End Sub
 
Upvote 0
I made a mistake try this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.OptionButton1.Value = True Then
For i = 1 To 31
    Sheets(i).Range("F1:J1").EntireColumn.Hidden = True
Next
End If
If ActiveSheet.OptionButton2.Value = True Then
For i = 1 To 31
    Sheets(i).Range("F1:J1").EntireColumn.Hidden = False
Next
End If
ActiveSheet.OptionButton2.Value = False
ActiveSheet.OptionButton1.Value = False
End Sub
 
Upvote 0
I made a mistake try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)If ActiveSheet.OptionButton1.Value = True ThenFor i = 1 To 31    Sheets(i).Range("F1:J1").EntireColumn.Hidden = TrueNextEnd IfIf ActiveSheet.OptionButton2.Value = True ThenFor i = 1 To 31    Sheets(i).Range("F1:J1").EntireColumn.Hidden = FalseNextEnd IfActiveSheet.OptionButton2.Value = FalseActiveSheet.OptionButton1.Value = FalseEnd Sub

Hi myans,

It's not working. :(
 
Last edited:
Upvote 0
Maybe, this untested code
Code:
Option ExplicitSub Worksheet_Change(ByVal Target As Range)Dim sh As WorksheetFor Each sh In WorksheetsIf Sheets("Sheet1").OptionButton1.Value = True Then    sh.Shapes.Columns("F:G").Hidden = TrueElse    sh.Shapes.Columns("F:G").Hidden = FalseEnd IfNext shEnd Sub
Hi alansidman,

Sorry, I forgot to mention that my option buttons are on a different sheet named "Main".

I need it to hide columns F:G on 31 other sheets named "1", "2", "3",..."31".

The actual sheet names correspond to the sheet number.

Thanks for the quick response tho.
 
Last edited:
Upvote 0
Do you know how to install sheet change event script?
And a sheet change event requires you to make some manual change in the sheet.
Clicking a option button is not a sheet change event.


Hi myans,

It's not working. :(
 
Upvote 0
I made a mistake try this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.OptionButton1.Value = True Then
For i = 1 To 31
    Sheets(i).Range("F1:J1").EntireColumn.Hidden = True
Next
End If
If ActiveSheet.OptionButton2.Value = True Then
For i = 1 To 31
    Sheets(i).Range("F1:J1").EntireColumn.Hidden = False
Next
End If
ActiveSheet.OptionButton2.Value = False
ActiveSheet.OptionButton1.Value = False
End Sub

You lost me at "install".
I'm sorry, I don't know anything. T^T
Do you know how to install sheet change event script?
And a sheet change event requires you to make some manual change in the sheet.
Clicking a option button is not a sheet change event.
 
Upvote 0
Code:
Option Explicit


Sub Worksheet_Change(ByVal Target As Range)


Dim sh As Worksheet
For Each sh In Worksheets
If ws.Name <> "Main" Then
If Sheets("Main").OptionButton1.Value = True Then
    sh.Shapes.Columns("F:G").Hidden = True
Else
    sh.Shapes.Columns("F:G").Hidden = False
End If
End If
Next sh
End Sub
 
Upvote 0
Here is a corrected code. I had a typo in the above code.

Code:
Sub Worksheet_Change(ByVal Target As Range)


Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name <> "Main" Then
If Sheets("Main").OptionButton1.Value = True Then
    sh.Shapes.Columns("F:G").Hidden = True
Else
    sh.Shapes.Columns("F:G").Hidden = False
End If
End If
Next sh
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
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