Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if an Excel range is hidden?

Tags:

excel

vba

In my code I include a boolean variable in which I want to assign the value of a range's hidden property. i.e. if the range is hidden, the variable should have the value true, and vice versa.

While running the code I get a '1004' Run-time error - Unable to get the Hidden property of the Range class. By this I assume that Hidden property in this case is write-only (correct me if I'm wrong).

Is there a way to determine (in my code, not by watching) whether a range/cell is hidden or not?

I have a class named "minas" and with this sub I am trying to create a collection of minas based on some criteria.

Public mines As Collection
Sub existing_months()
    Set mines = New Collection
    Dim min As minas
    Dim str As String
    Dim x As Range
    Dim y As Boolean
    For i = 1 To 12
        Set min = New minas
        Set x = Range("A1:A500").Find(i, LookIn:=xlValues, LookAt:=xlWhole)
        If x Is Nothing Then GoTo next_iteration:
        y = x.Hidden 'does not get the property
        Call min.initialize(x, y)
        str = min.minas & "/" & min.etos
        mines.Add min, str
        Debug.Print min.ref_range.Address & " " & min.end_cell
next_iteration:
    Next
    Set min = Nothing
End Sub
like image 720
mits Avatar asked Dec 30 '25 05:12

mits


2 Answers

You can say a cell is hidden if it is located on a hidden row or hidden column.
Then a range is hidden if all cells in that range are hidden:

Public Function IsHidden(rIn As Range) As Boolean
    Dim r As Range
    IsHidden = True
    For Each r In rIn
        If Not r.EntireRow.Hidden Then
            If Not r.EntireColumn.Hidden Then
                IsHidden = False
                Exit Function
            End If
        End If
    Next r
End Function
like image 140
Gary's Student Avatar answered Dec 31 '25 17:12

Gary's Student


According to a quick Google search, Range.Find will not find the data if the cell is hidden if you use LookIn:=xlValues. I tested this with "Test" in Cell A6 and hid the row. This code returned Nothing:

Sub TestIt()
    Dim x As Range
    Set x = Range("A1:A7").Find("Test", , xlValues, xlWhole)
    If x Is Nothing Then
        MsgBox "Nothing"
    Else
        If x.EntireRow.Hidden = True Then
            MsgBox x.Address & " is Hidden"
        Else
            MsgBox x.Address & " is Visible"
        End If
    End If
End Sub

Instead you need to use LookIn:=xlFormulas:

Sub TestIt()
    Dim x As Range
    Set x = Range("A1:A7").Find("Test", , xlFormulas, xlWhole)
    If x Is Nothing Then
        MsgBox "Nothing"
    Else
        If x.EntireRow.Hidden = True Then
            MsgBox x.Address & " is Hidden"
        Else
            MsgBox x.Address & " is Visible"
        End If
    End If
End Sub

Then you can use either:

y = x.EntireRow.Hidden

or

y = x.EntireColumn.Hidden

to get your Boolean (True if the cell is hidden and False if the cell is visible)

like image 37
Chrismas007 Avatar answered Dec 31 '25 19:12

Chrismas007



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!