Share via

VBA for hide automatically rows with empty cells

Anonymous
2014-01-03T14:54:07+00:00

Hi,

I'm trying to make a macro on vba (I'm a begginner) to hide automatically rows when the cells on column D are empty. The content of these cells is linked to other cells in the other sheet, so at any time may have content. Ie, are empty but if you put data in another sheet will no longer be empty and I would like to pass them visible automatically when this happened.

Anybody can help me?

Best regards

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-01-03T15:35:24+00:00

Hi,

So you have a simple link to another worksheet that returns zero of the cell is empty and you want to hide all rows that have a zero, is that correct? Yes, then try this code.

Private Sub Worksheet_Calculate()

Dim c As Range

For Each c In Range("D3:D100")

    If c.Value = 0 Then

        c.EntireRow.Hidden = True

    Else

        c.EntireRow.Hidden = False

    End If

Next

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-01-03T17:18:29+00:00

    I'm sorry but the macro doesn't work. I introduced the function "SUM" to a column (other one, not the D column) and the excel crash. This always happens every time I put the signal = "something" (content, function or operation) in any cell. I think that the macro is incompatible with something...

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-01-03T15:47:17+00:00

    Thank you very much for your help Mike. It results perfectly now :)

    Best regards

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-01-03T15:28:15+00:00

    Thank you Mike for the fast answer. The code doesn't hide the rows that I want. The content of the cells will be text, not numerical. It shows zero because the link to other cell. And it hide row 1 and I need it. The ideal is to hide in a determinate range, like row 3 to row 100.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-01-03T15:13:31+00:00

    Hi,

    Right click the sheet tab of the sheet where you want to hide the rows and view code. Paste the code below in on the right.

    Note that this doesn't hide all the rows on the sheet only those in the used range. Close VB editor and change a cell on the other sheet and the code will execute.

    Private Sub Worksheet_Calculate()

    Dim c As Range, LastRow As Long

    LastRow = Cells.Find(What:="*", After:=[A1], _

    SearchOrder:=xlByRows, _

    SearchDirection:=xlPrevious).Row

    For Each c In Range("D1:D" & LastRow)

        If Len(c.Value) > 0 Then

            c.EntireRow.Hidden = False

        Else

            c.EntireRow.Hidden = True

        End If

    Next

    End Sub

    Was this answer helpful?

    0 comments No comments