简体   繁体   中英

Hide a column if the content at row 2 is not equal to 'x', 'y', or 'z'

How can I look for content in the second row, and if it is not a "white listed" value, hide that column?

I have to look through multiple sheets that have columns at different locations (example: sheet 1 name column is A, sheet 2 name column is B).


Edit: Based on @ComradeMicha's comment I put something together, I'm sure its wrong but how can I make it work?

Sub Demo()

Dim arr(2) As String
Dim rng As Range: Set rng = Application.Range("Data!A2:CA2")
Dim cel As Range

arr(0) = "Name"
arr(1) = "Age"
arr(2) = "Gender"

For Each cel In rng.Cells
    With cel
        If Not IsInArray(cell, arr) Then
            Columns(cel).Hidden = True
        End If
    End With
Next cel
End Sub


Private Function IsInArray(valToBeFound As Variant, arr As Variant) As Boolean
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: Function to check if a value is in an array of values
'INPUT: Pass the function a value to search for and an array of values of any data type.
'OUTPUT: True if is in array, false otherwise
Dim element As Variant
On Error GoTo IsInArrayError: 'array is empty
    For Each element In arr
        If element = valToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next element
Exit Function
IsInArrayError:
On Error GoTo 0
IsInArray = False
End Function

This should do the trick if you are only looking for those 3 key words. It's not as fast as checking an array, but you are not looping through that many columns so the difference will be extremely trivial here.

Option Compare Text removes the case sensitive aspect of the text comparison. In other words, the macro will assume NAME = name .
Without this Option , NAME <> name

Option Explicit
Option Compare Text

Sub HideColumns()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Data")
Dim MyCell As Range
Dim HideMe As Range

Application.ScreenUpdating = False
    For Each MyCell In ws.Range("A2:CA2")
        If MyCell = "Name" Or MyCell = "Age" Or MyCell = "Gender" Then
            If HideMe Is Nothing Then
                Set HideMe = MyCell
            Else
                Set HideMe = Union(HideMe, MyCell)
            End If
        End If
    Next MyCell

    If Not HideMe Is Nothing Then
        HideMe.EntireColumn.Hidden = True
    End If
Application.ScreenUpdating = True

End Sub

Yes, you need VBA to automatically hide columns based on values.

Loop through each column in Row 2 (eg using this: https://www.excel-easy.com/vba/examples/loop-through-defined-range.html ), check if the cell value is in your whitelist (eg using this: https://wellsr.com/vba/2016/excel/check-if-value-is-in-array-vba/ ), and if it is not, hide the column:

Columns(i).hidden = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM