简体   繁体   English

如果第2行的内容不等于'x','y'或'z',则隐藏一列

[英]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). 我必须查看具有不同位置列的多个工作表(例如:工作表1名称列为A,工作表2名称列为B)。


Edit: Based on @ComradeMicha's comment I put something together, I'm sure its wrong but how can I make it work? 编辑:基于@ComradeMicha的评论,我将某些内容放在一起,我确定它是错误的,但是如何使它起作用?

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. 如果您只寻找这3个关键字,这应该可以解决问题。 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. Option Compare Text删除文本比较中区分大小写的部分。 In other words, the macro will assume NAME = name . 换句话说,宏将假定NAME = name
Without this Option , NAME <> name 如果没有此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. 是的,您需要VBA才能根据值自动隐藏列。

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: 循环浏览第2行中的每一列(例如,使用此命令: https : //www.excel-easy.com/vba/examples/loop-through-defined-range.html ),检查单元格值是否在您的白名单中(例如使用此方法: https : //wellsr.com/vba/2016/excel/check-if-value-is-in-array-vba/ ),如果不是,则隐藏该列:

Columns(i).hidden = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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