Case Statement for Multiple Checkboxes Hiding/Unhiding Columns

cmutschler

New Member
Joined
Feb 3, 2016
Messages
5
Hi! :)

I'm new to VBA and need some help changing this to a case statement. Thank you for your help!!! Cheryl


If ActiveSheet.Shapes("Check Box 110").ControlFormat.Value = 1 Then
Sheets("DHMO Copay").Columns("C").EntireColumn.Hidden = False
Else: Sheets("DHMO Copay").Columns("C").EntireColumn.Hidden = True
End If
If ActiveSheet.Shapes("Check Box 111").ControlFormat.Value = 1 Then
Sheets("DHMO Copay").Columns("D").EntireColumn.Hidden = False
Else: Sheets("DHMO Copay").Columns("D").EntireColumn.Hidden = True
End If
If ActiveSheet.Shapes("Check Box 122").ControlFormat.Value = 1 Then
Sheets("DHMO Copay").Columns("E").EntireColumn.Hidden = False
Else: Sheets("DHMO Copay").Columns("E").EntireColumn.Hidden = True
End If
If ActiveSheet.Shapes("Check Box 123").ControlFormat.Value = 1 Then
Sheets("DHMO Copay").Columns("F").EntireColumn.Hidden = False
Else: Sheets("DHMO Copay").Columns("F").EntireColumn.Hidden = True
End If

..... a bazillion more checkboxes
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
This loop may help reduce the amount of code, highlighted parts should be all you need to edit:
Rich (BB code):
    Dim x     As Long
    Dim arr() As Variant

    'Check box numbers
    arr = Array(110, 111, 122, 123)
  
    Application.ScreenUpdating = False
    
    With Sheets("DHMO Copay")
        .Cells(1, 3).Resize(, Ubound(arr) + 1).EntireColumn.Hidden = True
        For x = LBound(arr) To UBound(arr)
            If .Shapes("Check Box " & CStr(arr(x))).ControlFormat.Value Then .Cells(1, x + 3).EntireColumn.Hidden = False
        Next x
    End With
    
    Application.ScreenUpdating = True
    Erase arr
It assumes columns to be hidden always starts at column C and continues incrementally, e.g. C, D, E, F, G, etc.
 
Last edited:
Upvote 0
Try:
Code:
    Dim x     As Long
    Dim arr() As Variant
  
    arr = Array(110, 111, 122, 123)
  
    Application.ScreenUpdating = False
    
    With Sheets("DHMO Copay")
        .Cells(1, 3).Resize(, UBound(arr) + 1).EntireColumn.Hidden = True
        For x = LBound(arr) To UBound(arr)
            If .Shapes("Check Box " & CStr(arr(x))).ControlFormat.Value = 1 Then
                .Cells(1, x + 3).EntireColumn.Hidden = False
            End If
        Next x
    End With
    
    Application.ScreenUpdating = True
    Erase arr
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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