Hide Sheet

  • Thread starter Thread starter ranswert
  • Start date Start date
R

ranswert

What is the best way to hide everything above, below and the the right of a
range of cells? The range will change depending on what is selected.
Thanks
 
Here's some code for you

Sub HideAroundSelection()
Dim intRows As Integer
Dim intCols As Integer
Dim rngAbove As Range
Dim rngRight As Range
Dim rngBelow As Range
Dim rngLeft As Range


intRows = Selection.Rows.Count
intCols = Selection.Columns.Count
With Selection
Set rngAbove = .Cells(1, 1)
Set rngBelow = .Cells(1, 1).Offset(intRows - 1, 0)
Set rngRight = .Cells(1, 1).Offset(0, intCols - 1)
Set rngLeft = .Cells(1, 1)

If rngAbove.Row <> 1 Then
Range(rngAbove.Offset(-1, 0), .Cells(1, 1).Offset((1 - .Cells(1,
1).Row))).EntireRow.Hidden = True
End If
If rngBelow.Row <> ActiveSheet.Rows.Count Then
Range(rngBelow.Offset(1, 0),
rngBelow.Offset(ActiveSheet.Rows.Count - rngBelow.Row)).EntireRow.Hidden =
True
End If
If rngRight.Column <> ActiveSheet.Columns.Count Then
Range(rngRight.Offset(0, 1), rngRight.Offset(0,
ActiveSheet.Columns.Count - rngRight.Column)).EntireColumn.Hidden = True
End If
If rngLeft.Column <> 1 Then
Range(rngLeft.Offset(0, -1), rngLeft.Offset(0, 1 -
rngLeft.Column)).EntireColumn.Hidden = True
End If

End With
Set rngAbove = Nothing
Set rngRight = Nothing
Set rngBelow = Nothing
Set rngLeft = Nothing


End Sub
 
Thanks I'll give that a try

DomThePom said:
Here's some code for you

Sub HideAroundSelection()
Dim intRows As Integer
Dim intCols As Integer
Dim rngAbove As Range
Dim rngRight As Range
Dim rngBelow As Range
Dim rngLeft As Range


intRows = Selection.Rows.Count
intCols = Selection.Columns.Count
With Selection
Set rngAbove = .Cells(1, 1)
Set rngBelow = .Cells(1, 1).Offset(intRows - 1, 0)
Set rngRight = .Cells(1, 1).Offset(0, intCols - 1)
Set rngLeft = .Cells(1, 1)

If rngAbove.Row <> 1 Then
Range(rngAbove.Offset(-1, 0), .Cells(1, 1).Offset((1 - .Cells(1,
1).Row))).EntireRow.Hidden = True
End If
If rngBelow.Row <> ActiveSheet.Rows.Count Then
Range(rngBelow.Offset(1, 0),
rngBelow.Offset(ActiveSheet.Rows.Count - rngBelow.Row)).EntireRow.Hidden =
True
End If
If rngRight.Column <> ActiveSheet.Columns.Count Then
Range(rngRight.Offset(0, 1), rngRight.Offset(0,
ActiveSheet.Columns.Count - rngRight.Column)).EntireColumn.Hidden = True
End If
If rngLeft.Column <> 1 Then
Range(rngLeft.Offset(0, -1), rngLeft.Offset(0, 1 -
rngLeft.Column)).EntireColumn.Hidden = True
End If

End With
Set rngAbove = Nothing
Set rngRight = Nothing
Set rngBelow = Nothing
Set rngLeft = Nothing


End Sub
 
Sub UnHideAllRowsAndColumns()
With ActiveSheet.Cells
.EntireRow.Hidden = False
.EntireColumn.Hidden = False
End With
End Sub

To work this out yourself just record a macro and then change references to
selection
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top