SOLVED
Home

Hide a set of columns

%3CLINGO-SUB%20id%3D%22lingo-sub-151348%22%20slang%3D%22en-US%22%3EHide%20a%20set%20of%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-151348%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20using%20Excel%20for%20Mac%20version%2016.9%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMy%20company%20provides%20me%20a%20huge%20dataset%20in%20the%20form%20of%20an%20.xlsx%20...%20it%20has%20152%20columns%20all%20the%20way%20out%20to%20%22EV%22.%26nbsp%3B%26nbsp%3B%20I%20need%20maybe%20a%20couple%20dozen%20of%20these.%26nbsp%3B%26nbsp%3B%20The%20data%20is%20re-published%20in%20the%20source%20format%20every%20few%20weeks%20and%20I%20take%20about%20ten%20sheets%20with%20a%20handful%20of%20rows%20and%20each%20with%20the%20same%20set%20of%20152%20columns.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20every%20couple%20of%20weeks%20I%20need%20to%20touch%20ten%20sheets%20%22hide%20columns%22%20until%20I%20see%20only%20%3CEM%3Ethese%20columns%3C%2FEM%3E.%3C%2FP%3E%0A%3CPRE%3EC%2CH%2CN-R%2CAE-AG%2CAI%2CBQ-BR%2CCM-CO%2CDB%2CDE-DF%2CDN-DO%2CEB-ED%2CEG%2CEJ%2CES-ST%3C%2FPRE%3E%0A%3CP%3EIs%20there%20a%20way%20I%20can%20apply%20this%20set%20to%3C%2FP%3E%0A%3CP%3Ea)%20a%20new%20sheet%20when%20I%20open%20it%3C%2FP%3E%0A%3CP%3Eb)%20all%20or%20bunch%20of%20sheets%20in%20a%20workbook%3C%2FP%3E%0A%3CP%3Ec)%20a%20sheet%20I%20have%20open%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%2C%3C%2FP%3E%0A%3CP%3E--jg%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-151348%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-154362%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20a%20set%20of%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-154362%22%20slang%3D%22en-US%22%3EAh!%20%20I%20see%20you%20added%20Sheet.Activate%20there%20in%20the%20loop.%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20is%20not%20only%20very%20helpful%20for%20my%20current%20problem%2C%20I've%20learned%20a%20lot%20of%20things%20about%20writing%20macros.%20%20%20Very%20powerful%20stuff!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-154358%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20a%20set%20of%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-154358%22%20slang%3D%22en-US%22%3E%3CP%3EJames%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESorry%20about%20this%2C%20I've%20updated%20the%20code%20with%20the%20fix%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESub%20ShowOnlySpecificColumns1()%0A'Written%20by%20Haytham%20Amairah%0A'Last%20Updated%3A%202%2F9%2F2018%0A'Applied%20to%20all%20worksheets%20in%20the%20active%20workbook.%0A%0A%20%20%20For%20Each%20Sheet%20In%20Sheets%0A%20%20%20%20Sheet.Activate%0A%20%20%20%20Cells.Select%0A%20%20%20%20Selection.EntireColumn.Hidden%20%3D%20True%0A%20%20%20%20Range(%22C%3AC%2C%20H%3AH%2C%20N%3AR%2C%20AE%3AAG%2C%20AI%3AAI%2C%20BQ%3ABR%2CCM%3ACO%2C%20DB%3ADB%2C%20DE%3ADF%2C%20DN%3ADO%2C%20EB%3AED%2C%20EG%3AEG%2CEJ%3AEJ%2C%20ES%3AST%22)%20_%0A%20%20%20%20.EntireColumn.Hidden%20%3D%20False%0A%20%20%20%20Range(%22C1%22).Select%0A%20%20%20Next%20Sheet%0A%20%20%20%20%0AEnd%20Sub%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20goal%20of%20this%20line%20is%20to%20save%20the%20active%20cell%20from%20drowning%20or%20disappearing%20with%20the%20columns%20that%20will%20be%20hidden!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3ERange(%22C1%22).Select%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EIt%20moves%20the%20active%20cell%20to%20the%20shore%2C%20which%20is%20the%20first%20visible%20cell%20of%20each%20worksheet!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ETry%20to%20delete%20it%2C%20and%20see%20what%20happens!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-154344%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20a%20set%20of%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-154344%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Haytham%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20seems%20that%20the%26nbsp%3BShowOnlySpecificColumns1%20which%20should%20apply%20to%20all%20sheets%20in%20a%20workbook%20only%20applies%20to%20the%20currently%20selected%20sheet.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20see%20that%20you%20have%20%22for%20each%20-%20next%22%20logic%20in%20there%2C%20it%20just%20doesn't%20appear%20to%20work.%26nbsp%3B%20Any%20clue%20as%20to%20why%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20what%20is%20the%20purpose%20of%20the%20line%20%22Range(%22C1%22).Select%22%20line%20in%20each%20of%20these%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20again!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-151924%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20a%20set%20of%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-151924%22%20slang%3D%22en-US%22%3EThank%20you!%20%20%20This%20is%20brilliant.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-151582%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20a%20set%20of%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-151582%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20James%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20have%20to%20use%20some%20lines%20of%20code%20to%20automate%20this%20task%2C%20you%20can%20record%20all%20these%20steps%20through%20the%20%3CA%20href%3D%22https%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2Fautomate-tasks-with-the-macro-recorder-974ef220-f716-4e01-b015-3ea70e64937b%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EMacro%20Recorder%3C%2FA%3E%20to%20create%20a%20macro%20of%20this%20task%20so%20that%20you%20can%20reuse%20this%20Macro%20over%20and%20over%20again!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnyway%2C%20I've%20created%20the%20following%20two%20codes%20for%20you%20so%20that%20you%20can%20save%20them%20in%20the%20VBA%20code%26nbsp%3Bmodule%2C%20and%20start%20using%20them%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%231%20(%3CSPAN%3EApplied%20to%20all%20worksheets%20in%20the%20active%20workbook%3C%2FSPAN%3E)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CPRE%3ESub%20ShowOnlySpecificColumns1()%0A'Written%20by%20Haytham%20Amairah%0A'Last%20Updated%3A%202%2F2%2F2018%0A'Applied%20to%20all%20worksheets%20in%20the%20active%20workbook.%0A%0A%20%20%20For%20Each%20Sheet%20In%20Sheets%0A%20%20%20%20Cells.Select%0A%20%20%20%20Selection.EntireColumn.Hidden%20%3D%20True%0A%20%20%20%20Range(%22C%3AC%2C%20H%3AH%2C%20N%3AR%2C%20AE%3AAG%2C%20AI%3AAI%2C%20BQ%3ABR%2CCM%3ACO%2C%20DB%3ADB%2C%20DE%3ADF%2C%20DN%3ADO%2C%20EB%3AED%2C%20EG%3AEG%2CEJ%3AEJ%2C%20ES%3AST%22)%20_%0A%20%20%20%20.EntireColumn.Hidden%20%3D%20False%0A%20%20%20Next%20Sheet%0A%20%20%20%20%0A%20%20%20Range(%22C1%22).Select%0A%20%20%20%20%0AEnd%20Sub%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%232%20(%3CSPAN%3EApplied%20to%20the%20active%20worksheet%3C%2FSPAN%3E)%3C%2FSTRONG%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CSTRONG%3ESub%20ShowOnlySpecificColumns2()%0A'Written%20by%20Haytham%20Amairah%0A'Last%20Updated%3A%202%2F2%2F2018%0A'Applied%20to%20the%20active%20worksheet.%0A%0A%20%20%20%20Cells.Select%0A%20%20%20%20Selection.EntireColumn.Hidden%20%3D%20True%0A%20%20%20%20Range(%22C%3AC%2C%20H%3AH%2C%20N%3AR%2C%20AE%3AAG%2C%20AI%3AAI%2C%20BQ%3ABR%2CCM%3ACO%2C%20DB%3ADB%2C%20DE%3ADF%2C%20DN%3ADO%2C%20EB%3AED%2C%20EG%3AEG%2CEJ%3AEJ%2C%20ES%3AST%22)%20_%0A%20%20%20%20.EntireColumn.Hidden%20%3D%20False%0A%20%20%20%20%0A%20%20%20Range(%22C1%22).Select%0A%20%20%20%20%0AEnd%20Sub%3C%2FSTRONG%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20I%20recommend%20you%20to%20save%20them%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2Fcreate-and-save-all-your-macros-in-a-single-workbook-66c97ab3-11c2-44db-b021-ae005a9bc790%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EPersonal%20Macro%20Workbook%3C%2FA%3E%20so%20that%20you%20can%20use%20them%26nbsp%3Bin%20other%20workbooks%20and%3CSPAN%3E%20make%20them%20available%20every%20time%20you%20open%20Excel.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENOTE%3A%20The%20solution%20above%20is%20applied%20to%20%3CSPAN%20class%3D%22appliesToItem%20x-hidden-focus%22%3EExcel%20for%26nbsp%3BWindows%2C%20but%20I%20think%20it's%20also%20applied%20to%20Excel%20for%20Mac%2C%20please%20try%20it%20and%20provide%20me%20with%20any%20feedback.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
James Gill
New Contributor

I'm using Excel for Mac version 16.9

 

My company provides me a huge dataset in the form of an .xlsx ... it has 152 columns all the way out to "EV".   I need maybe a couple dozen of these.   The data is re-published in the source format every few weeks and I take about ten sheets with a handful of rows and each with the same set of 152 columns.

 

So every couple of weeks I need to touch ten sheets "hide columns" until I see only these columns.

C,H,N-R,AE-AG,AI,BQ-BR,CM-CO,DB,DE-DF,DN-DO,EB-ED,EG,EJ,ES-ST

Is there a way I can apply this set to

a) a new sheet when I open it

b) all or bunch of sheets in a workbook

c) a sheet I have open

 

Thanks,

--jg

5 Replies
Highlighted
Solution

Hi James,

 

You have to use some lines of code to automate this task, you can record all these steps through the Macro Recorder to create a macro of this task so that you can reuse this Macro over and over again!

 

Anyway, I've created the following two codes for you so that you can save them in the VBA code module, and start using them:

 

#1 (Applied to all worksheets in the active workbook)

Sub ShowOnlySpecificColumns1()
'Written by Haytham Amairah
'Last Updated: 2/2/2018
'Applied to all worksheets in the active workbook.

   For Each Sheet In Sheets
    Cells.Select
    Selection.EntireColumn.Hidden = True
    Range("C:C, H:H, N:R, AE:AG, AI:AI, BQ:BR,CM:CO, DB:DB, DE:DF, DN:DO, EB:ED, EG:EG,EJ:EJ, ES:ST") _
    .EntireColumn.Hidden = False
   Next Sheet
    
   Range("C1").Select
    
End Sub

 

#2 (Applied to the active worksheet) 

Sub ShowOnlySpecificColumns2()
'Written by Haytham Amairah
'Last Updated: 2/2/2018
'Applied to the active worksheet.

    Cells.Select
    Selection.EntireColumn.Hidden = True
    Range("C:C, H:H, N:R, AE:AG, AI:AI, BQ:BR,CM:CO, DB:DB, DE:DF, DN:DO, EB:ED, EG:EG,EJ:EJ, ES:ST") _
    .EntireColumn.Hidden = False
    
   Range("C1").Select
    
End Sub

 

But I recommend you to save them in the Personal Macro Workbook so that you can use them in other workbooks and make them available every time you open Excel.

 

NOTE: The solution above is applied to Excel for Windows, but I think it's also applied to Excel for Mac, please try it and provide me with any feedback.

Thank you! This is brilliant.

Hello Haytham,

 

It seems that the ShowOnlySpecificColumns1 which should apply to all sheets in a workbook only applies to the currently selected sheet.

 

I see that you have "for each - next" logic in there, it just doesn't appear to work.  Any clue as to why?

 

Also, what is the purpose of the line "Range("C1").Select" line in each of these?

 

Thanks again!

James,

 

Sorry about this, I've updated the code with the fix:

 

Sub ShowOnlySpecificColumns1()
'Written by Haytham Amairah
'Last Updated: 2/9/2018
'Applied to all worksheets in the active workbook.

   For Each Sheet In Sheets
    Sheet.Activate
    Cells.Select
    Selection.EntireColumn.Hidden = True
    Range("C:C, H:H, N:R, AE:AG, AI:AI, BQ:BR,CM:CO, DB:DB, DE:DF, DN:DO, EB:ED, EG:EG,EJ:EJ, ES:ST") _
    .EntireColumn.Hidden = False
    Range("C1").Select
   Next Sheet
    
End Sub

 

The goal of this line is to save the active cell from drowning or disappearing with the columns that will be hidden!

Range("C1").Select

It moves the active cell to the shore, which is the first visible cell of each worksheet!

Try to delete it, and see what happens!

Ah! I see you added Sheet.Activate there in the loop.

This is not only very helpful for my current problem, I've learned a lot of things about writing macros. Very powerful stuff!
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
48 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies