Hiding Rows if empty

cata2200

Board Regular
Joined
Jul 29, 2006
Messages
60
Hi
I'm working on a sheet to estimate materials usage. My sheet is spreaded over 3 pages. After finishing I want to print the sheet, but without the empty rows from between, so the report will be compact in, maybe, one or two pages.
In column A I have Serial Numbers for materials, or, from time to time, separators for material type.

Any help?

Regards
Catalin
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
This code works wonders:

Sub HideRows()
' ***********************************************************************************************
' Date: September 15, 2002
' Description: Allows a user to select a couple of cells and hides any rows in that particular row which are not used.
' ***********************************************************************************************

Range("A1").Select
On Error GoTo BYE
Count = 0
Application.ScreenUpdating = False
Application.Cursor = xlWait
' ExcelLastCell is what Excel thinks is the last cell
Set ExcelLastCell = ActiveSheet.Cells.SpecialCells(xlLastCell)

' Determine the last row with data in it (must also copy above para for this to work)
LastRowWithData = ExcelLastCell.Row
Row = ExcelLastCell.Row
Do While Application.CountA(ActiveSheet.Rows(Row)) = 0 And Row <> 1
Row = Row - 1
Loop
LastRowWithData = Row ' Row number

Set rng = Intersect(Rows(1), Selection.EntireColumn).EntireColumn
MySelectedColumns = rng.Address ' Returns columns contained in selection in form $D:$G or if only one $F:$F
ColonPos = InStr(MySelectedColumns, ":")
FirstSelectedColumn = Mid(MySelectedColumns, 2, ColonPos - 2)
FirstColumnNumber = Range(FirstSelectedColumn & "1").Column
LastSelectedColumn = Mid(MySelectedColumns, ColonPos + 2, Len(MySelectedColumns) - (ColonPos + 1))
LastColumnNumber = Range(LastSelectedColumn & "1").Column

NumOfSelectedCols = Val(LastColumnNumber) - (Val(FirstColumnNumber) - 1)
StartingCol = FirstColumnNumber

StartingRow = ActiveCell.Row + 1

For i = StartingRow To LastRowWithData
For j = 0 To NumOfSelectedCols - 1
Cells(i, StartingCol + j).Select
If Len(ActiveCell.Value) > 0 Then
GoTo NEXT_ROW
End If
Next
' ********** If we get here neither row had any data in that column ******
Selection.EntireRow.Hidden = True
Count = Count + 1
Application.StatusBar = " . . . . . . . . . . . . . . . . . . Hiding" & Str(Count) & " Rows which don't contain data for selected rows!"
NEXT_ROW:
Next

Application.ScreenUpdating = True
Application.Cursor = xlDefault
Cells(StartingRow, 1).Select
Application.StatusBar = ""
Range("D1").Select

BYE:
End Sub


Change the Range reference to a column where the cells will be blank in the rows you wish to hide.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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