Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Hiding sheets
Hello All,
I have numerous sheets and I would like to show or hide them based on the month. IE. If it is Jan hide all my other sheets apart from the Jan sheet, this is to stop people seeing the data. I got the following code from an MVP from this site. But I don't know what to do with it. I'm a beginner in Vb so please bear with me!! Dim MyMonth As Integer MyMonth = Month(Now()) Select Case MyMonth Case 1 'If month number is 1 (Jan) Sheets("Feb").Visible = False Sheets("Mar").Visible = False Case 2 'If Month number is 2 (Feb) Sheets("Jan").Visible = False Sheets("March").Visible = False Case 3 'If Month number is 3 (Mar) Sheets("Jan").Visible = False Sheets("Mar").Visible = True Sheets("Feb").Visible = False End Select End Sub The Expert suggested I put this in the Open workbook event??? Where is that please? Thank for reading Natalie |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Hiding sheets
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks), 'select View Code from the menu, and then enter Private Sub Workbook_Open() .... your code End Sub -- HTH RP (remove nothere from the email address if mailing direct) "Natalie" wrote in message ... Hello All, I have numerous sheets and I would like to show or hide them based on the month. IE. If it is Jan hide all my other sheets apart from the Jan sheet, this is to stop people seeing the data. I got the following code from an MVP from this site. But I don't know what to do with it. I'm a beginner in Vb so please bear with me!! Dim MyMonth As Integer MyMonth = Month(Now()) Select Case MyMonth Case 1 'If month number is 1 (Jan) Sheets("Feb").Visible = False Sheets("Mar").Visible = False Case 2 'If Month number is 2 (Feb) Sheets("Jan").Visible = False Sheets("March").Visible = False Case 3 'If Month number is 3 (Mar) Sheets("Jan").Visible = False Sheets("Mar").Visible = True Sheets("Feb").Visible = False End Select End Sub The Expert suggested I put this in the Open workbook event??? Where is that please? Thank for reading Natalie |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Hiding sheets
Here is smoe code to do the hiding thing that you want.
Option Explicit Private Sub Workbook_Open() Dim wks As Worksheet Dim strCurrentMonth As String strCurrentMonth = MonthString For Each wks In Worksheets If wks.Name < strCurrentMonth Then wks.Visible = xlSheetHidden Next wks End Sub Private Function MonthString() As String Dim strMonth As String Dim intMonth As Integer intMonth = Month(Date) Select Case intMonth Case 1 strMonth = "Jan" Case 2 strMonth = "Feb" Case 3 strMonth = "Mar" Case 4 strMonth = "Apr" Case 5 strMonth = "May" Case 6 strMonth = "Jun" Case 7 strMonth = "Jul" Case 8 strMonth = "Aug" Case 9 strMonth = "Sep" Case 10 strMonth = "Oct" Case 11 strMonth = "Nov" Case 12 strMonth = "Dec" End Select MonthString = strMonth End Function Paste this into the ThisWorkbook object in the VB Editor. If you want to modify your own code the Workbook_Open event is found in the ThisWorkbook object. Click on the Drop down that says General and where it says Declarations select the event you want... Try both my code and yours and play with them a bit. You can step through them using the F8 key... HTH "Natalie" wrote: Hello All, I have numerous sheets and I would like to show or hide them based on the month. IE. If it is Jan hide all my other sheets apart from the Jan sheet, this is to stop people seeing the data. I got the following code from an MVP from this site. But I don't know what to do with it. I'm a beginner in Vb so please bear with me!! Dim MyMonth As Integer MyMonth = Month(Now()) Select Case MyMonth Case 1 'If month number is 1 (Jan) Sheets("Feb").Visible = False Sheets("Mar").Visible = False Case 2 'If Month number is 2 (Feb) Sheets("Jan").Visible = False Sheets("March").Visible = False Case 3 'If Month number is 3 (Mar) Sheets("Jan").Visible = False Sheets("Mar").Visible = True Sheets("Feb").Visible = False End Select End Sub The Expert suggested I put this in the Open workbook event??? Where is that please? Thank for reading Natalie |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Hiding sheets
Jim,
You probably need to modify that Open code for subsequent runs, when only one sheet is showing, and the month changes Private Sub Workbook_Open() Dim wks As Worksheet Dim strCurrentMonth As String strCurrentMonth = MonthString For Each wks In Worksheets If wks.Name = strCurrentMonth Then wks.Visible = xlSheetVisible Else wks.Visible = xlSheetHidden End If Next wks End Sub -- HTH RP (remove nothere from the email address if mailing direct) "Jim Thomlinson" wrote in message ... Here is smoe code to do the hiding thing that you want. Option Explicit Private Sub Workbook_Open() Dim wks As Worksheet Dim strCurrentMonth As String strCurrentMonth = MonthString For Each wks In Worksheets If wks.Name < strCurrentMonth Then wks.Visible = xlSheetHidden Next wks End Sub Private Function MonthString() As String Dim strMonth As String Dim intMonth As Integer intMonth = Month(Date) Select Case intMonth Case 1 strMonth = "Jan" Case 2 strMonth = "Feb" Case 3 strMonth = "Mar" Case 4 strMonth = "Apr" Case 5 strMonth = "May" Case 6 strMonth = "Jun" Case 7 strMonth = "Jul" Case 8 strMonth = "Aug" Case 9 strMonth = "Sep" Case 10 strMonth = "Oct" Case 11 strMonth = "Nov" Case 12 strMonth = "Dec" End Select MonthString = strMonth End Function Paste this into the ThisWorkbook object in the VB Editor. If you want to modify your own code the Workbook_Open event is found in the ThisWorkbook object. Click on the Drop down that says General and where it says Declarations select the event you want... Try both my code and yours and play with them a bit. You can step through them using the F8 key... HTH "Natalie" wrote: Hello All, I have numerous sheets and I would like to show or hide them based on the month. IE. If it is Jan hide all my other sheets apart from the Jan sheet, this is to stop people seeing the data. I got the following code from an MVP from this site. But I don't know what to do with it. I'm a beginner in Vb so please bear with me!! Dim MyMonth As Integer MyMonth = Month(Now()) Select Case MyMonth Case 1 'If month number is 1 (Jan) Sheets("Feb").Visible = False Sheets("Mar").Visible = False Case 2 'If Month number is 2 (Feb) Sheets("Jan").Visible = False Sheets("March").Visible = False Case 3 'If Month number is 3 (Mar) Sheets("Jan").Visible = False Sheets("Mar").Visible = True Sheets("Feb").Visible = False End Select End Sub The Expert suggested I put this in the Open workbook event??? Where is that please? Thank for reading Natalie |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Hiding sheets
Good point... Go with Bob's code... Mine would be unhappy when the month
rolled over... No visisble sheets can be a problem... :) "Bob Phillips" wrote: Jim, You probably need to modify that Open code for subsequent runs, when only one sheet is showing, and the month changes Private Sub Workbook_Open() Dim wks As Worksheet Dim strCurrentMonth As String strCurrentMonth = MonthString For Each wks In Worksheets If wks.Name = strCurrentMonth Then wks.Visible = xlSheetVisible Else wks.Visible = xlSheetHidden End If Next wks End Sub -- HTH RP (remove nothere from the email address if mailing direct) "Jim Thomlinson" wrote in message ... Here is smoe code to do the hiding thing that you want. Option Explicit Private Sub Workbook_Open() Dim wks As Worksheet Dim strCurrentMonth As String strCurrentMonth = MonthString For Each wks In Worksheets If wks.Name < strCurrentMonth Then wks.Visible = xlSheetHidden Next wks End Sub Private Function MonthString() As String Dim strMonth As String Dim intMonth As Integer intMonth = Month(Date) Select Case intMonth Case 1 strMonth = "Jan" Case 2 strMonth = "Feb" Case 3 strMonth = "Mar" Case 4 strMonth = "Apr" Case 5 strMonth = "May" Case 6 strMonth = "Jun" Case 7 strMonth = "Jul" Case 8 strMonth = "Aug" Case 9 strMonth = "Sep" Case 10 strMonth = "Oct" Case 11 strMonth = "Nov" Case 12 strMonth = "Dec" End Select MonthString = strMonth End Function Paste this into the ThisWorkbook object in the VB Editor. If you want to modify your own code the Workbook_Open event is found in the ThisWorkbook object. Click on the Drop down that says General and where it says Declarations select the event you want... Try both my code and yours and play with them a bit. You can step through them using the F8 key... HTH "Natalie" wrote: Hello All, I have numerous sheets and I would like to show or hide them based on the month. IE. If it is Jan hide all my other sheets apart from the Jan sheet, this is to stop people seeing the data. I got the following code from an MVP from this site. But I don't know what to do with it. I'm a beginner in Vb so please bear with me!! Dim MyMonth As Integer MyMonth = Month(Now()) Select Case MyMonth Case 1 'If month number is 1 (Jan) Sheets("Feb").Visible = False Sheets("Mar").Visible = False Case 2 'If Month number is 2 (Feb) Sheets("Jan").Visible = False Sheets("March").Visible = False Case 3 'If Month number is 3 (Mar) Sheets("Jan").Visible = False Sheets("Mar").Visible = True Sheets("Feb").Visible = False End Select End Sub The Expert suggested I put this in the Open workbook event??? Where is that please? Thank for reading Natalie |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Hiding sheets
And one more...
Option Explicit Sub auto_open() Dim iCtr As Long Dim CurMonthName As String CurMonthName = Format(Date, "mmm") On Error Resume Next 'just in case of naming problems 'show the current month Worksheets(CurMonthName).Visible = True For iCtr = 1 To Worksheets.Count If LCase(Worksheets(iCtr).Name) = LCase(CurMonthName) Then 'do nothing--it's already shown Else Worksheets(iCtr).Visible = False End If Next iCtr On Error GoTo 0 End Sub I think you'll find it's better to show the one worksheet first. Then hide all the others. If you get unlucky, you maybe hiding a worksheet that was the only visible sheet. If that happens, then your code will break, since there always has to be at least one visible sheet in your workbook. Are you any relation to JudithJubilee? She asked a similar question recently. Natalie wrote: Hello All, I have numerous sheets and I would like to show or hide them based on the month. IE. If it is Jan hide all my other sheets apart from the Jan sheet, this is to stop people seeing the data. I got the following code from an MVP from this site. But I don't know what to do with it. I'm a beginner in Vb so please bear with me!! Dim MyMonth As Integer MyMonth = Month(Now()) Select Case MyMonth Case 1 'If month number is 1 (Jan) Sheets("Feb").Visible = False Sheets("Mar").Visible = False Case 2 'If Month number is 2 (Feb) Sheets("Jan").Visible = False Sheets("March").Visible = False Case 3 'If Month number is 3 (Mar) Sheets("Jan").Visible = False Sheets("Mar").Visible = True Sheets("Feb").Visible = False End Select End Sub The Expert suggested I put this in the Open workbook event??? Where is that please? Thank for reading Natalie -- Dave Peterson |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Hiding sheets
Thankyou all for your help. I have it working now!!!
Natalie -----Original Message----- Hey, I think I can help with this! Open up VB (from Excel) by clicking Alt+F11. You should see a list of your worksheets on the Project Explorer pane. It's usually named "Project - VBAProject" and resides on the upper left corner. If you don't see the Project Explorer go to View--Project Explorer. After the worksheets you should see a workbook by the name of "ThisWorkbook". Double-click this guy and paste your code in this window (wrapped around the following code): Private Sub Workbook_SheetCalculate(ByVal Sh As Object) <Your (or MVPs) code here End Sub Hope this helps, -Fabricio "Natalie" wrote: Hello All, I have numerous sheets and I would like to show or hide them based on the month. IE. If it is Jan hide all my other sheets apart from the Jan sheet, this is to stop people seeing the data. I got the following code from an MVP from this site. But I don't know what to do with it. I'm a beginner in Vb so please bear with me!! Dim MyMonth As Integer MyMonth = Month(Now()) Select Case MyMonth Case 1 'If month number is 1 (Jan) Sheets("Feb").Visible = False Sheets("Mar").Visible = False Case 2 'If Month number is 2 (Feb) Sheets("Jan").Visible = False Sheets("March").Visible = False Case 3 'If Month number is 3 (Mar) Sheets("Jan").Visible = False Sheets("Mar").Visible = True Sheets("Feb").Visible = False End Select End Sub The Expert suggested I put this in the Open workbook event??? Where is that please? Thank for reading Natalie . |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
hiding zero values on all sheets & by default on new sheets | Excel Worksheet Functions | |||
Hiding sheets | Excel Discussion (Misc queries) | |||
Locking Sheets / Hiding Sheets | Excel Worksheet Functions | |||
Hiding Sheets | Excel Worksheet Functions | |||
Hiding Sheets | Excel Programming |