You are here:

Excel/Hiding rows with blank value

Advertisement


Question

sample
QUESTION: The spreadsheet containing a list box which enable user to drill down by Region and the results will show by countries. Where North America Region could contain only two country and Latin America Region has 18 countries.

In Cell C5 is a list box
and returning value in range B11:J37 and B44:J69.

For example:    C5
Issuing Region:   NORTH_AMERICA                     
B11     B12    
USA     500
CANADA   700                     


My question is how can I write a vb or macro to automatically hide the rows that are blank when the user choose a region they want to see.

I hope you could help me with this question.

Many thanks.

Regards,
Sian



ANSWER: Sian,

Let's assume that column B can be used to determine which rows are blank - i.e. if a cell in column B is blank, then the row needs to be hidden.  Assume that values in column B are constants and if a cell appears blank, it is, in fact, empty.  Then you can use code like this:


Range("B:B").SpecialCells(xlBlanks).EntireRow.Visible = False

or

Range("B2",Cells(rows.count,"B").End(xlup).SpecialCells(xlBlanks).EntireRow.Visible = False


Are the type of macro you can use if you can depend on column B to accurately reflect which rows are blank or not - or use a column that can.


Hope that is what you were asking.

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Many thanks for your quick response, Tom. As you can tell, I am totally clueless in VB at all, but my boss is driving me insane!!  On one hand wanted me to know everything, on the other hand refuse to send me to taking any classes.  I am learning myself through books and online.
I realised that I should have asked how could I hide and unhide when user select from a drop down list.  I have attached a png file to show you my worksheet.  Hopefully this can further explain what I want to do. Please let me know if this explain well or how could I further show you what my intend is.

Thank you once again.

Answer
I assume your dropdown is done using the List option from data validation.

If so, you can react to a selection by using the worksheet change event.  Right click on the sheet tab and select view code.


in the resulting module (the sheet module), in the top of the module are two dropdowns.  In the left dropdown, select Worksheet and in right dropdown select Change

that should put in a declaration for the Change event in the module:

=============ERROR HERE===============================

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

===============should be============================

Private Sub Worksheet_Change(ByVal Target As Range)

End Sub

======================================================

[ Added NOTE:]
[I warned you against using the selectionchange vice the change event and then accidentally pasted in the declaration for the SelectionChange event.  I have changed it in this revision.  The code below was and is correct.  It has the change event.  I noticed the mistake, but when I got around to sending it, I forgot to correct the above. ]




The argument Target in the event will carry a reference to the cell or cells that changed by editing or code (or selection from the dropdown) and caused the event to trigger.  


If Target.Address = "$C$5" then
 ' the cell with the dropdown triggered the event.
 ' now we need to react to the selection in the cell


The problem is that you haven't identified how you are equating North_America to  the countries of the USA and CANADA.  I would suggest you add a sheet in your workbook and put in the equivalencies

A1:  NORTH_AMERICA
B1:  USA
C1:  11
A2:  NORTH_AMERICA
B2:  CANADA
C2:  12
A3:  Another Region Name
B3:  First country in that region
C3:  Row of that country

Continue listing your data going down columns A:C


assume that is in a sheet named TABLE

now we can use that list to determine which rows to unhide:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, cell As Range
If Target.Address = "$C$5" Then
 If Len(Trim(Target)) = 0 Then
   ' if C5 is blank, then show all rows
   Me.Range("B11:B37,B44:B69").EntireRow.Hidden = False
   Exit Sub
 End If

 With Worksheets("TABLE")
   Set r = .Range(.Cells(1, "A"), .Cells(Rows.Count, "A").End(xlUp))
 End With
 ' hide all rows in your data ranges
 Me.Range("B11:B37,B44:B69").EntireRow.Hidden = True
 For Each cell In r
  If LCase(cell.Value) = LCase(Me.Range("C5").Value) Then
     ' region matches C7 in this sheet, show row for the
     ' row number stored in column C of TABLE
     Me.Rows(cell.Offset(0, 2)).EntireRow.Hidden = False
  End If
 Next
End If
End Sub


In the sheet module the reference "me" is a predefined reference to the sheet that contains the code module - in this case, the sheet you show in your picture.

The above code was tested using the details you provided and it worked as I expected you want it to work.  Test it on a copy of your workbook.  Just to Emphasize that the code does depend on a Table, just as I have described, exists in a worksheet named TABLE beginning in cell A1.   

If you delete the entry in column C, then the code will unhide all rows in the specified data range.

--
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

Tom Ogilvy

Expertise

Worked with the program for many years - provided assistance on MS Excel Newsgroups since 1997. Have received the Microsoft MVP award annually since 1999. I don't answer questions on using Excel in a browser Since I have no way to test this. Prefer not to answer charting questions. I consider myself to be particularly knowledgeable about using VBA internal to Excel but have no problems with formulas and pivot tables either.

Experience

Have Used Excel for 15 - 20 years. Answered in excess of 70,000 Excel related questions in MS Excel newsgroups. Unless obvious, please specify whether you want a worksheet function or macro/VBA solution.

Education/Credentials
BS General Engineering (concentration in Industrial Engineering) MS Operations Research Systems Analysis

©2009 About.com, a part of The New York Times Company. All rights reserved.