Hide/Unhide columns

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys, Need advice to modify the code to use Cell B1 as toggle button to hide and unhide the columns.
Right now, when click B1, it hides the columns but I have to move cursor to other cell & click once to open columns.

VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

  Select Case Sh.Name
    Case "Data"
      Exit Sub
    Case Else
  End Select

  If g_blnWbkShtSelChange Then Exit Sub
  If Selection.Count = 1 Then
    If Not Intersect(Target, Range("B1")) Is Nothing Then
      g_blnWbkShtSelChange = True
        Columns("C:G").EntireColumn.Hidden = True
    Else
        Columns("C:G").EntireColumn.Hidden = False
   End If
   End If

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You talk about 'clicking' on B1, but you are using the SelectionChange event. Selection is different. So while this will work

VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    
Select Case Sh.Name
        Case "Data"
            Exit Sub
        Case Else
    End Select

    If Target.Address = "$B$1" Then
        If Columns("C:G").EntireColumn.Hidden Then
            Columns("C:G").EntireColumn.Hidden = False
        Else
            Columns("C:G").EntireColumn.Hidden = True
        End If
    End If
End Sub

It requires that the cell selection change to toggle, which means you have to move the selection on and off B1. In other words, you cannot just stay on Cell B1 and click. To do that, you are better off using the SheetBeforeDoubleClick event:

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    
    Select Case Sh.Name
        Case "Data"
            Exit Sub
        Case Else
    End Select

    If Target.Address = "$B$1" Then
        If Columns("C:G").EntireColumn.Hidden Then
            Columns("C:G").EntireColumn.Hidden = False
        Else
            Columns("C:G").EntireColumn.Hidden = True
        End If
    End If
    Application.SendKeys ("{ESC}")
End Sub
 
Upvote 0
Hello Vincent88,
I don't know any way to do that, seems imposible.
Also don't know how far you can to go out of basic question,
but you can try to use this the trick. Use difference between active and selected cell.
This code selects two dislocated cells, but "B1" is stays active cell.

Insert this to the standard module. This is switching procedure.
VBA Code:
Public g_blnWbkShtSelChange As Boolean

Sub ToggleButtonSwitch()

    If g_blnWbkShtSelChange = False Then
       g_blnWbkShtSelChange = True
    Else
       g_blnWbkShtSelChange = False
    End If
   
End Sub
Now insert this in the Workbook_SheetSelectionChange event.
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    If Not Selection.Address = "$B$1" Then Exit Sub

    Call ToggleButtonSwitch
   
    If g_blnWbkShtSelChange Then
        Union([B1], [Z1]).Select
        [B1].Activate
        Columns("C:G").EntireColumn.Hidden = True
    Else
        Union([B1], [Z1]).Select
        [B1].Activate
        Columns("C:G").EntireColumn.Hidden = False
    End If

End Sub
If the range [Z1] is in use, you can replace him with some unuseble range.
Hope so this is a litle bit closer to the answer.
 
Upvote 0
Solution
Hi riv01 and EXCEL MAX, all your codes work well. Thanks a lot
 
Upvote 0
We are glad that you are satisfied and that you finally found a solution.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
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