You are here:

Excel/Macro for Hiding Rows

Advertisement


Question
print screen of worksheet
print screen of worksh  
Hi Tom

I've found a former macro you offered for hiding rows and need some help with modification please.

Sub HideRows()
Dim cell As Range, rng As Range
Cells.Rows.Hidden = False
On Error Resume Next
Set rng = Columns(3).SpecialCells(xlFormulas, xlNumbers)
On Error GoTo 0
For Each cell In rng
If cell.Value = 0 Then
   cell.EntireRow.Hidden = True
End If
Next
End Sub

It ALMOST does what I want... !

The macro hides the rows in my column 3 where the value=0 but I actually want them hidden only if the cell value is blank (there is a formula in the cell that will leave it blank under certain circumstances).

There are two solutions I can think of:

Solution 1:  if cell is blank
-----------------------------
Change the IF to cell.value = BLANK.  However, there are other cells in the same column (under headings, with totals etc.) that are also blank that I don't want hidden.

Solution 2:  if cell equals cell immediately above
--------------------------------------------------
The table I'm working with repeats the last-referenced row of look-up information for the remainder of a table. (You'll see from the attached image that the formula result in Col C remains blank - if that cell equals the one above - and I could just copy the formula to all following columns, but then I'm back at the above problem:  blank cells, rows not hidden.)

How can I change the macro so that the entire row is hidden:
  if cell.value Rx= cell R(x-1)

Definitely open to any other easier way!

Thank you for any help you can offer.
Sharon.

Answer
Sharon,


Sub HideRows()
Dim cell As Range, rng As Range
Cells.Rows.Hidden = False
For Each cell In Range("C12", cells(rows.count, "D").End(xlup).offset(0,-1))
If cell.Value = cell.offset(-1,0)  Then
  cell.EntireRow.Hidden = True
End If
Next
End Sub

should hide rows 33 to 38  (I believe it is 33, could be 32 - the second blank cell in column C at the bottom)

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.