SOLVED

Hide columns using check boxes

%3CLINGO-SUB%20id%3D%22lingo-sub-1083962%22%20slang%3D%22en-US%22%3EHide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1083962%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20new%20to%20Excel%20programming%20but%20seem%20to%20be%20making%20progress%20and%20am%20enjoying%20the%20power%20and%20flexibility%20of%20VBA.%20I%20have%20a%20spreadsheet%20that%20has%20two%20sheets.%20Sheet1%20contains%2025%20columns%20of%20data.%20At%20any%20given%20time%20I%20only%20want%20to%20display%20a%20few%20of%20them%2C%20so%20Sheet2%20has%2025%20check%20boxes%20that%20hides%2Funhides%20the%20desired%20columns%20on%20Sheet1.%20All%20works%20well%20until%20I%20move%20a%20column%20on%20Sheet1.%20The%20check%20box%20and%20code%20to%20hide%2Funhide%20the%20column%20doesn't%20reference%20its%20new%20column%20position%20but%2C%20instead%2C%20references%20its%20original%20column%20position.%20Here's%20the%20code%20from%20one%20of%20the%20check%20boxes%20on%20Sheet2%20that%20hides%2Funhides%20column%20G%20on%20Sheet1.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20Range(%22A8%22)%20%3D%20%22False%22%20Then%20%26nbsp%3B%20%2F%2FA8%20on%20Sheet2%20is%20set%20to%20true%20or%20false%20by%20its%20corresponding%20check%20box%26nbsp%3B%3C%2FP%3E%3CP%3ESheets(%22Sheet1%22).Columns(%22G%22).EntireColumn.Hidden%20%3D%20True%20%26nbsp%3B%20%2F%2Fif%20not%20checked%20the%20column%20is%20hidden%3C%2FP%3E%3CP%3EElse%3C%2FP%3E%3CP%3ESheets(%22Sheet1%22).Columns(%22G%22).EntireColumn.Hidden%20%3D%20False%20%26nbsp%3B%20%2F%2Fif%20checked%20the%20column%20is%20visible%3C%2FP%3E%3CP%3EEnd%20If%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20for%20the%20hide%2Funhide%20code%20to%20reference%20the%20same%20column%20no%20matter%20where%20it%20is%20moved%20by%20the%20user%3F%3C%2FP%3E%3CP%3EMany%20thanks%20for%20any%20help%20or%20suggestions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1083962%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1083979%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1083979%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F502429%22%20target%3D%22_blank%22%3E%40jumpcut%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20it%20work%20as%20desired%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EDim%20ws1%20As%20Worksheet%0ADim%20ws2%20As%20Worksheet%0A%0ASet%20ws1%20%3D%20Worksheets(%22Sheet1%22)%0ASet%20ws2%20%3D%20Worksheets(%22Sheet2%22)%0A%0Aws1.Columns(%22G%22).Hidden%20%3D%20Not%20ws2.Range(%22A8%22)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1083986%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1083986%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%3C%2FP%3E%3CP%3EHello%20and%20Thank%20You%20for%20your%20reply.%20When%20I%20check%20or%20uncheck%20the%20check%20box%20I%20get%20the%20following%20dialog%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERun-time%20error%20'9'%3A%3C%2FP%3E%3CP%3ESubscript%20out%20of%20range%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1083991%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1083991%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F502429%22%20target%3D%22_blank%22%3E%40jumpcut%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETweak%20the%20Sheet%20Names%20in%20the%20following%20lines%20as%20per%20the%20actual%20sheet%20names%20in%20the%20workbook...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESet%20ws1%20%3D%20Worksheets(%22Sheet1%22)%0ASet%20ws2%20%3D%20Worksheets(%22Sheet2%22)%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20ws1%20i.e.%20Sheet1%20is%20the%20Sheet%20where%20you%20want%20to%20hide%20the%20columns%20and%20ws2%20i.e.%20Sheet2%20is%20the%20Sheet%20which%20contains%20the%20linked%20Cells%20which%20turn%20to%20True%20or%20False%20by%20clicking%20on%20CheckBoxes.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1084016%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1084016%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOf%20course%2C%20I%20should%20have%20realized%20the%20names%20needed%20to%20change.%20Now%2C%20when%20I%20run%20this%20code%20everything%20works%20as%20before.%20Column%20G%2C%20which%20I%20have%20named%20%22Notes%22%20is%20visible%2Fhidden%20by%20the%20check%20box.%20But%20if%20I%20move%20the%20%22Notes%22%20(G)%20column%20to%20another%20position%20(column%20C%2C%20for%20example)%20the%20check%20box%20still%20hides%2Funhides%20column%20G%20instead%20of%20the%20%22Notes%22%20column%20in%20its%20new%20position%20column%20C.%3C%2FP%3E%3CP%3EThank%20you%2C%20again%2C%20for%20your%20reply.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1084047%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1084047%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F502429%22%20target%3D%22_blank%22%3E%40jumpcut%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20that%20case%2C%20you%20should%20first%20locate%20a%20column%20with%20header%20%22Notes%22%20on%20Sheet1%20and%20if%20it%20is%20found%2C%20you%20hide%20or%20unhide%20it%20irrespective%20of%20which%20column%20contains%20that%20header.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20try%20it%20like%20this%20and%20don't%20forget%20to%20tweak%20the%20Sheet%20Names%20in%20the%20code...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EDim%20ws1%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20ws2%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20headerCell%20%20As%20Range%0A%0ASet%20ws1%20%3D%20Worksheets(%22Sheet1%22)%0ASet%20ws2%20%3D%20Worksheets(%22Sheet2%22)%0A%0A'Assuming%20headers%20are%20in%20Row1%20on%20Sheet1%0ASet%20headerCell%20%3D%20ws1.Rows(1).Find(what%3A%3D%22Notes%22%2C%20lookat%3A%3DxlWhole)%0A%0AIf%20Not%20headerCell%20Is%20Nothing%20Then%0A%20%20%20%20headerCell.EntireColumn.Hidden%20%3D%20Not%20ws2.Range(%22A8%22)%0AElse%0A%20%20%20%20MsgBox%20%22The%20column%20with%20header%20'Notes'%20was%20not%20found.%22%2C%20vbExclamation%0A%20%20%20%20Exit%20Sub%0AEnd%20If%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1084465%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1084465%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWorks%20perfectly!%20Many%20thanks%20for%20your%20help.%20A%20very%20Happy%20New%20Year%20to%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1084983%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20columns%20using%20check%20boxes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1084983%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F502429%22%20target%3D%22_blank%22%3E%40jumpcut%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou're%20welcome!%20Glad%20it%20worked%20as%20desired.%3C%2FP%3E%3CP%3EHappy%20New%20Year%20to%20you%20too!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I'm new to Excel programming but seem to be making progress and am enjoying the power and flexibility of VBA. I have a spreadsheet that has two sheets. Sheet1 contains 25 columns of data. At any given time I only want to display a few of them, so Sheet2 has 25 check boxes that hides/unhides the desired columns on Sheet1. All works well until I move a column on Sheet1. The check box and code to hide/unhide the column doesn't reference its new column position but, instead, references its original column position. Here's the code from one of the check boxes on Sheet2 that hides/unhides column G on Sheet1.

 

If Range("A8") = "False" Then   //A8 on Sheet2 is set to true or false by its corresponding check box 

Sheets("Sheet1").Columns("G").EntireColumn.Hidden = True   //if not checked the column is hidden

Else

Sheets("Sheet1").Columns("G").EntireColumn.Hidden = False   //if checked the column is visible

End If

 

Is there a way for the hide/unhide code to reference the same column no matter where it is moved by the user?

Many thanks for any help or suggestions.

 

7 Replies
Highlighted

@jumpcut 

Does it work as desired?

 

Dim ws1 As Worksheet
Dim ws2 As Worksheet

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

ws1.Columns("G").Hidden = Not ws2.Range("A8")
Highlighted

@Subodh_Tiwari_sktneer

Hello and Thank You for your reply. When I check or uncheck the check box I get the following dialog:

 

Run-time error '9':

Subscript out of range

 

 

Highlighted

@jumpcut 

Tweak the Sheet Names in the following lines as per the actual sheet names in the workbook...

 

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

 

Here ws1 i.e. Sheet1 is the Sheet where you want to hide the columns and ws2 i.e. Sheet2 is the Sheet which contains the linked Cells which turn to True or False by clicking on CheckBoxes.

Highlighted

@Subodh_Tiwari_sktneer 

Of course, I should have realized the names needed to change. Now, when I run this code everything works as before. Column G, which I have named "Notes" is visible/hidden by the check box. But if I move the "Notes" (G) column to another position (column C, for example) the check box still hides/unhides column G instead of the "Notes" column in its new position column C.

Thank you, again, for your reply.

Highlighted
Solution

@jumpcut 

In that case, you should first locate a column with header "Notes" on Sheet1 and if it is found, you hide or unhide it irrespective of which column contains that header.

 

Please try it like this and don't forget to tweak the Sheet Names in the code...

Dim ws1         As Worksheet
Dim ws2         As Worksheet
Dim headerCell  As Range

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

'Assuming headers are in Row1 on Sheet1
Set headerCell = ws1.Rows(1).Find(what:="Notes", lookat:=xlWhole)

If Not headerCell Is Nothing Then
    headerCell.EntireColumn.Hidden = Not ws2.Range("A8")
Else
    MsgBox "The column with header 'Notes' was not found.", vbExclamation
    Exit Sub
End If
Highlighted

@Subodh_Tiwari_sktneer 

Works perfectly! Many thanks for your help. A very Happy New Year to you!

Highlighted

@jumpcut 

You're welcome! Glad it worked as desired.

Happy New Year to you too!