Excel VBA unhide columns across multiple sheets with click in cell

Stuepef

Board Regular
Joined
Oct 23, 2017
Messages
128
Office Version
  1. 2021
Platform
  1. Windows
I would like to add a macro to my workbook that unhides columns across 3 worksheets when I click in A1 on any of those worksheets. Here is what I have thus far:

Macro to Unhide columns across those sheets:
Code:
Sub OpenColumnsSummary()
Dim wsMySheet As Worksheet
   Application.ScreenUpdating = False
   For Each wsMySheet In Sheets(Array(Sheet6.Name, Sheet7.Name, Sheet8.Name))
      With wsMySheet
         .Columns("J:M").EntireColumn.Hidden = False
         .Columns("N:Q").EntireColumn.Hidden = False
         .Columns("R:U").EntireColumn.Hidden = False
         .Columns("V:Y").EntireColumn.Hidden = False
         .Columns("Z:AC").EntireColumn.Hidden = False
         .Columns("AD:AG").EntireColumn.Hidden = False
      End With
   Next wsMySheet
   Application.ScreenUpdating = True
End Sub

I am looking for some guidance on how I can execute this code when A1 is clicked on any of sheets 6, 7 or 8.

Thank you!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Do you want the code to work on the sheet you click, on all three regardless of which was clicked?
 
Upvote 0
@Fluff - I would like the macro to work on all worksheets regardless of which was clicked if possible.
 
Upvote 0
In that case try
Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
   Application.ScreenUpdating = False
   If Target.Address(0, 0) <> "A1" Then Exit Sub
   Cancel = True
   If Sh.CodeName = "Sheet6" Or Sh.CodeName = "Sheet7" Or Sh.CodeName = "Sheet8" Then
      For Each wsMySheet In Sheets(Array(Sheet6.Name, Sheet7.Name, Sheet8.Name))
         With wsMySheet
            .Columns("J:M").EntireColumn.Hidden = False
            .Columns("N:Q").EntireColumn.Hidden = False
            .Columns("R:U").EntireColumn.Hidden = False
            .Columns("V:Y").EntireColumn.Hidden = False
            .Columns("Z:AC").EntireColumn.Hidden = False
            .Columns("AD:AG").EntireColumn.Hidden = False
         End With
      Next wsMySheet
   End If
   Application.ScreenUpdating = True
End Sub
This needs to go in the ThisWorkbook module & you will need to doubleclick A1
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
@Fluff - Is it possible to add the same code except change the target to A4 to hide the same columns? I tried doing that myself within the ThisWorkBook, but when I update the code and sub name, it changes it to General instead of Workbook. Can I not have two codes in the ThisWorkBook? Here is what I attempted:
Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)   
Application.ScreenUpdating = False
   If Target.Address(0, 0) <> "A4" Then Exit Sub
   Cancel = True
   If Sh.CodeName = "Sheet6" Or Sh.CodeName = "Sheet7" Or Sh.CodeName = "Sheet8" Then
      For Each wsMySheet In Sheets(Array(Sheet6.Name, Sheet7.Name, Sheet8.Name))
         With wsMySheet
         .Columns("J:M").EntireColumn.Hidden = .Range("K20").Value = ""
         .Columns("N:Q").Hidden = .Range("O20").Value = ""
         .Columns("R:U").Hidden = .Range("S20").Value = ""
         .Columns("V:Y").Hidden = .Range("W20").Value = ""
         .Columns("Z:AC").Hidden = .Range("AA20").Value = ""
         .Columns("AD:AG").Hidden = .Range("AE20").Value = ""
         End With
      Next wsMySheet
   End If
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
You cannot two event codes of the same type.
Try
Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
   Application.ScreenUpdating = False
   If Sh.CodeName = "Sheet6" Or Sh.CodeName = "Sheet7" Or Sh.CodeName = "Sheet8" Then
      If Target.Address(0, 0) = "A1" Then
         Cancel = True
         For Each wsMySheet In Sheets(Array(Sheet6.Name, Sheet7.Name, Sheet8.Name))
            With wsMySheet
               .Columns("J:M").EntireColumn.Hidden = False
               .Columns("N:Q").EntireColumn.Hidden = False
               .Columns("R:U").EntireColumn.Hidden = False
               .Columns("V:Y").EntireColumn.Hidden = False
               .Columns("Z:AC").EntireColumn.Hidden = False
               .Columns("AD:AG").EntireColumn.Hidden = False
            End With
         Next wsMySheet
      ElseIf Target.Address(0, 0) = "A4" Then
         Cancel = True
         For Each wsMySheet In Sheets(Array(Sheet6.Name, Sheet7.Name, Sheet8.Name))
            With wsMySheet
            .Columns("J:M").EntireColumn.Hidden = .Range("K20").Value = ""
            .Columns("N:Q").Hidden = .Range("O20").Value = ""
            .Columns("R:U").Hidden = .Range("S20").Value = ""
            .Columns("V:Y").Hidden = .Range("W20").Value = ""
            .Columns("Z:AC").Hidden = .Range("AA20").Value = ""
            .Columns("AD:AG").Hidden = .Range("AE20").Value = ""
            End With
         Next wsMySheet
      End If
   End If
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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