Select & hide first and last rows of data

kernelku

New Member
Joined
Aug 14, 2017
Messages
7
What I'm trying to accomplish:
Select everything between row 2 and the final row that contains data, across all columns, and hide it.

Purpose:
I routinely need to print just the first 2 rows, plus the final row of data on workbooks that vary in size.

Example of raw data:
1
2
3
4
5
6

Example of final outcome:
1
2
6


And it is worth noting that the number of columns and rows will vary greatly and I need to be able to execute this on multiple workbooks on-the-fly.

Thanks for the help!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Is there one column that is always populated that we can use to determine where the data ends?
That is, for every row with data, is column A (or some other column) always populated?

If so, then you can use code like this:
Code:
Sub MyHideRowsMacro()

    Dim lr As Long
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   If last row is after row 3, then hide rows
    If lr > 3 Then
        Rows("3:" & lr - 1).EntireRow.Hidden = True
    End If

End Sub
 
Upvote 0
Perfection. Easy to understand, clean.

Thanks again. Something so simple can really make the day-to-day feel less laborious.
 
Upvote 0
You are welcome!
Glad I was able to help.
:)
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

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