Solved

Excel Macro - Hide Columns based header row

Posted on 2014-03-06
12
1,112 Views
Last Modified: 2014-03-06
Hi,

I have various spreadsheets which have somewhere between 100 and 200 columns and many rows containing event data. I need to come up with a way to hide all columns which do not match criteria in row 1. Example

Columns
First_name
Last_name
Sex
Age
so on and so forth

I am looking to hide all but First_name and Last_name for the sake of the example.
0
Comment
Question by:Minot
  • 6
  • 5
12 Comments
 
LVL 13

Expert Comment

by:Santosh Gupta
ID: 39910494
i not clear what do you want... but it seems you can achieve your goal by Filter option.
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39910509
Sub HideColumns()

Dim currentSheet As Worksheet
Dim totalSheets As Integer
Dim totalCols As Integer
Dim colsToHide As String
Dim cellR As Range

colsToHide = "First_Name,Last_Name"

totalSheets = ThisWorkbook.Sheets.Count




For i = 1 To totalSheets

    Set currentSheet = ThisWorkbook.Sheets(i)
    totalCols = currentSheet.Columns.Count

    For colIndex = 1 To totalCols
   
        Set cellR = currentSheet.Cells(1, colIndex)
                       
        If (InStr(colsToHide, cellR, vbTextCompare) > 0) Then
                       
            cellR.EntireColumn.Hidden = True
           
       
        End If
       
       
   
    Next colIndex

Next i


End Sub
0
 

Author Comment

by:Minot
ID: 39910520
This needs to be in a macro so it can be played against multiple files, doing this as a first step to format data for printing, I want to hide requisite columns then, sort by last name, set page width to 1 page, landscape and print etc. I have all but hiding the columns. I will attach a xls showing the current columns. I want to hide all but a select few, I highlighted them in yellow.
Book1.xlsx
0
 

Author Comment

by:Minot
ID: 39910540
getting a type mismatch on

  If (InStr(colsToHide, cellR, vbTextCompare) > 0) Then
0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 500 total points
ID: 39910546
Attached file
Copy-of-Book1.xlsm
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39910547
Remove vbTextCompare
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Minot
ID: 39910588
still cannot get it to run, removed the compare. Also took your sheet, added Phone to the mix and ran again, did not hide phone.  Truly I am trying to hide all but first, last, etc.
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39910635
You have add like  "[phone]". Also what is the error you are getting?
0
 

Author Comment

by:Minot
ID: 39910728
I think the issue is that my macros are in personal.xlsb thus they are playing on personal not the active sheet.  reviewing that now.
0
 

Author Comment

by:Minot
ID: 39910759
ok progress, this seems to work

Sub HideColumns()
Dim currentSheet As Worksheet
Dim totalSheets As Integer
Dim totalCols As Integer
Dim colsToHide As String
Dim cellR As Range

colsToHide = "[First_Name],[Last_Name],[Email Address],[Phone],[Phone Area Code]"

totalSheets = ThisWorkbook.Sheets.Count




For i = 1 To totalSheets

    Set currentSheet = ActiveWorkbook.ActiveSheet
    totalCols = currentSheet.Columns.Count

    For colIndex = 1 To totalCols
   
        Set cellR = currentSheet.Cells(1, colIndex)
       
        If (cellR = "") Then
            Exit Sub
        End If
                       
        If (InStr(colsToHide, "[" + cellR + "]") > 0) Then
                       
            cellR.EntireColumn.Hidden = False
        Else
            cellR.EntireColumn.Hidden = True
           
       
        End If
       
       
   
    Next colIndex

Next i


End Sub
0
 

Author Closing Comment

by:Minot
ID: 39911383
Thank you for the help, this solution works well.
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39911484
If you are just doing it for active sheet (just one sheet) then you do not need the sheets for loop.

Set currentSheet = ActiveWorkbook.ActiveSheet
    totalCols = currentSheet.Columns.Count

    For colIndex = 1 To totalCols
   
        Set cellR = currentSheet.Cells(1, colIndex)
       
        If (cellR = "") Then
            Exit Sub
        End If
                       
        If (InStr(colsToHide, "[" + cellR + "]") > 0) Then
                       
            cellR.EntireColumn.Hidden = False
        Else
            cellR.EntireColumn.Hidden = True
           
       
        End If
       
       
   
    Next colIndex
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now