Hiding/Unhiding sheets and rows (An easy one for most of you....)

  • Thread starter Thread starter Dean Goodmen
  • Start date Start date
D

Dean Goodmen

I have three sheets in my workbook and under certain circumstance I
need to hide/unhide and entire sheet, or just certain rows. Could some
one give me an example on how to code a line to do this?

Thanks!
 
This hide's a sheet if OK is pressed:

Sub HideSheet()
Dim hideit
hideit = MsgBox("Press OK to hide", vbOKCancel, "Hide Test")

If hideit = 1 Then
Worksheets("Sheet1").Visible = False
Else
Worksheets("Sheet1").Visible = True
End If
End Sub



This does the same with Column C

Sub HideCol()
Dim hideit
hideit = MsgBox("Press OK to hide", vbOKCancel, "Hide Test")

If hideit = 1 Then
Columns("C:C").EntireColumn.Hidden = True
Else
Columns("C:C").EntireColumn.Hidden = False
End If

End Sub

HTH
Steve
 
add this to the code for sheet1.make sure that you have a worksheet called
sheet2

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
If Target.Value = 0 Then
SheetHide "Sheet2"
Else
SheetUnhide "Sheet2"
End If
End If
End Sub

Sub SheetHide(sheetname As String)
Worksheets(sheetname).Visible = False
End Sub
Sub SheetUnhide(sheetname As String)
Worksheets(sheetname).Visible = True
End Sub

type 0 (zero) into A1 of sheet1 and sheet2 will be hidden. type anything
else into A1 of sheet1 and sheet2 will be visible

TIP: Use the macro-recorder & examine the code - it will be a big help.
Usually the code will need to be tweaked to get it just so though.

HTH

Patrick Molloy
Microsoft Excel MVP
 
That works great; however how would you hide multiple rows, or a range
of them?

I tried Rows("2:5").Entirerow.Hidden = True, but it did not work...
 

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