Using VB to hide sections of spreadsheet

Dark0Prince

Active Member
Joined
Feb 17, 2016
Messages
433
I want to be able to hide sections of my spreadsheet. From let's say the GJ section to the end of the GJ section. But I don't know how to define the end of the GJ section because new data keeps moving the rows down and changing how big the section is. This code below works but as soon as I insert another row this will not grab the whole section anymore because it is now bigger. Is there a way I can define the end of the section. I'm using command buttons for this code

Sub GJ()
'
' GJ Macro
' UNSHRINKS ALL SECTIONS>HIDES ALL SECTIONS THAT YOU DON'T WANT TO SEE
'

'
Cells.Select
Selection.EntireRow.Hidden = False
Rows("2:2").Select
ActiveWindow.SmallScroll Down:=519
Rows("2:539").Select
Selection.EntireRow.Hidden = True
ActiveWindow.SmallScroll Down:=309
Rows("861:861").Select
ActiveWindow.SmallScroll Down:=1143
Rows("861:2000").Select
Selection.EntireRow.Hidden = True
ActiveWindow.SmallScroll Down:=-465
Range("A1").Select
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi Dark0Prince,

You'll have to tell us what defines the start and end of the 'GJ' section?

Cheers,
Alan.
 
Upvote 0
Thanks for the quick response well I could define them with a cell that contains the text startGJ and endGJ and then I could just hide the text in the cells.
 
Upvote 0
If it's a contiguous block of cells the "CurrentRegion" property may work.

In a standard module:
Code:
Public Sub Test()

ActiveCell.CurrentRegion.Interior.ColorIndex = 3

End Sub
 
Upvote 0
Hi Dark0Prince,

This will hide everything between the cell where it finds startGJ and the cell where it finds endGJ

Code:
Sub HideRowsBetween()

SRow = Cells.Find("startGJ").Row
ERow = Cells.Find("endGJ").Row
Rows(SRow & ":" & ERow).EntireRow.Hidden = True

End Sub

Hope this helps,
Cheers,
Alan.
 
Upvote 0
Hi Dark0Prince,

This will hide everything between the cell where it finds startGJ and the cell where it finds endGJ

Code:
Sub HideRowsBetween()

SRow = Cells.Find("startGJ").Row
ERow = Cells.Find("endGJ").Row
Rows(SRow & ":" & ERow).EntireRow.Hidden = True

End Sub

Hope this helps,
Cheers,
Alan.

I wanted to hide everything but those range of rows. I may have said it in reverse I'm sorry if I did.
 
Upvote 0
Ah right ok, this should do the trick for that

Code:
Sub HideRowsOutside()

LRow = Range("[B][COLOR=#FF0000]A[/COLOR][/B]" & Rows.Count).End(xlUp).Row
SRow = Cells.Find("startGJ").Row
ERow = Cells.Find("endGJ").Row

Rows([B][COLOR=#0000FF]1[/COLOR][/B] & ":" & SRow).EntireRow.Hidden = True
Rows(ERow & ":" & LRow).EntireRow.Hidden = True

End Sub

This assumes you have data in Column A, if not change the red 'A' to a column you will have data in. Also if you have a header row that you want to keep change the blue '1' to '2' to keep the first row (or however many rows you would like to keep...)

Cheers,
Alan.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,492
Members
448,967
Latest member
visheshkotha

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