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
 

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,334
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:

cmutschler

New Member
Joined
Feb 3, 2016
Messages
5
Wow. Thank you very much!

Giving this a go, any checkbox that I check unhides all columns.
 

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,334
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:

Forum statistics

Threads
1,082,383
Messages
5,365,142
Members
400,825
Latest member
Sreekanth_21

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top