2

I am currently trying to hide certain columns if Row(8:8) meets certain criteria. My For Each loop is currently not working. Could someone point me in the right direction?

Sub hide()
' hide Macro
'
Dim rng As Range

    For Each rng In Range("F:BJ").Columns
        If Rows.Range("8") = "Test" Or Rows.Range("8") = "Test1" Then
            Column.rng.EntireColumn.Hidden = True
        End If

    Next rng
End Sub 

3 Answers 3

3

You can do it this way:

Dim rng As Range
For Each rng In Range("F8:BJ8")

    If rng.Value = "Test" Or rng.Value = "Test1" Then
        rng.EntireColumn.Hidden = True
    End If

Next rng
Sign up to request clarification or add additional context in comments.

2 Comments

@Bond - I realize my response is very close to yours but I will leave it be as it removes case sensitivity and toggles the column back to visible if the value has changed.
@Jeeped - The more the merrier, if it helps the OP.
2

Presumably, you would want to unhide the columns if the value in row 8 was changed programmatically or otherwise.

Dim rng As Range
With Worksheets("Sheet1")
    For Each rng In .Range("F8:BJ8")
        rng.EntireColumn.Hidden = _
            CBool(LCase(rng.Value) = "test" Or LCase(rng.Value) = "test1")
    Next rng
End With

Comments

1

The Rows and Columns ranges refer to the whole spreadsheet if you don't specify a range.

Sub hideColumn()

    Dim rng As Range

    For Each rng In Range("F:BJ").Columns
        If rng.Rows(8) = "Test" Or rng.Rows(8) = "Test1" Then
            rng.EntireColumn.Hidden = True
        End If
    Next rng

End Sub 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.