Code to hide rows

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I need some code to hide some rows if some cells are blank.

  • If C13 is blank, I need to hide row 25
  • if C14 is blank I need to hide row 26
  • If C15 is blank I need to hide row 27
  • If C16 is blank, I need to hide row 28

Can someone help me with the code please?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
This was one of my earliest VBA questions when I first started using it ~20 years ago :) The basic idea is something like this:

Code:
Public Sub HideRows()
  Dim rng As Excel.Range

  For Each rng In ActiveSheet.Range("C13:16")
    If IsEmpty(rng) Then
      rng.Offset(RowOffset:=25-13).EntireRow.RowHeight = 0
    Else
      rng.Offset(RowOffset:=25-13).EntireRow.AutoFit
    End If
  Next rng
End Sub
 
Upvote 0
Thanks for the reply.

With that code I get the error message of application defined or object defined error.
 
Upvote 0
I found some code that only hides the first row. What is it missing so it hides the other rows?

  • Dim r As Range
  • Application.EnableEvents = False
  • For Each r In Me.Range("C13:C16")
  • If r.Value = 0 Then
  • r.EntireRow.Hidden = True
  • Else
  • r.EntireRow.Hidden = False
  • End If
  • Next r
  • Application.EnableEvents = True
 
Upvote 0
Maybe this way.....but @iliace has missed a col ref in the range...
Range("C13:16")


VBA Code:
Public Sub HideRows()
  Dim cell As Range, n As Long
n = 25
  For Each cell In Range("C13:C16")
    If IsEmpty(cell) Then
      Rows(n).Hidden = True
    End If
    n = n + 1
  Next cell
End Sub
 
Upvote 0
Probably need this line:
For Each rng In ActiveSheet.Range("C13:16")

changed to:

For Each rng In ActiveSheet.Range("C13:C16")
 
Upvote 0
Try this one
VBA Code:
Sub HideRows()
  Dim rw As Range
  
  For Each rw In ActiveSheet.Rows("13:16")
    If rw.Cells(, "C") = Empty Then
      rw.Offset(12).EntireRow.Hidden = True
    Else
      rw.Offset(12).EntireRow.Hidden = False
    End If
  Next rw
End Sub
 
Upvote 0
Those code samples both only hide row 25 if C13 is blank. Even if the other rows are blank (C14:C16), rows 26:28 will not get hidden. If there is a value in C13 but not C14,15 or 16, only row 26 gets hidden, not the remaining two that should be hidden.
 
Upvote 0
My code works fine for me !!
Are the cells TRULY blank in the C13:C16 range OR the result of formulas ??
 
Upvote 0
Maybe try it this way as well...
VBA Code:
Sub HideRows()
Dim cell As Range, n As Long
n = 25
  For Each cell In Range("C13:C16")
    If cell.Value = "" Then Rows(n).Hidden = True
    n = n + 1
  Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,324
Messages
6,124,250
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top