Sunday, February 5, 2012

Hide Worksheets automatically on open

I was playing on MrExcel.com a while ago and someone wanted to know how they could keep links active on their new year's sheet but not have all the old years hanging around for people to click on. Someone else suggested he just hide the old year's sheets and he wanted to know if he could do that automatically which led me to write this code that might be helpful for many times you want to hide sheets for one reason or another. This code hides any sheet that is named for a year that has already passed. It runs when the workbook opens and has an error check to make sure that it doesn't try to hide every sheet in the workbook (can't have that!!!).
I can think of many uses for this and it wouldn't just have to be sheet names. You could have it check cell A1 on every sheet and hide the sheets that have a specific entry, or you could put dates in cell A1 and then as the workbook opens, the code would compare the dates in cell A1with today and only unhide those sheets that are for that day. You could have a workbook that many people use and have them enter their name into an input box and then have it hide every worksheets except that user's. I could go on and on ;) Anyway here is that code (you would put this in the Workbook module):

Thursday, January 5, 2012

My first blog!! Excel macro :)

I had a problem with users of my Excel worksheet at work saving copies of my file leading to some people entering data into the real file and some people entering data into the copy – BIG mess!! Today I thought of this solution: On workbook open, I assign the pathway of the workbook to a variable and then compare that pathway to the pathway where the real workbook resides. If they don’t match, a message pops up and tells them it’s just a copy and then closes without letting them enter any data. I had to use the UCase function because I found out that depending on what computer the user is using, the pathway was capitalized differently – by using UCase, it doesn’t matter how things are capitalized J  Here’s the code to copy:  (Put this in the Workbook object code)

Option Explicit
Private Sub Workbook_Open()
Dim myfullfilename As String
myfullfilename = ActiveWorkbook.FullName 'get the pathway of the current workbook
'The actual file pathway should be typed below in all caps, this is just an example file pathway
If UCase(myfullfilename) <> _
 "J:\SHARED DRIVE\EXCEL CODE\RESTRICT OPENING FROM OTHER PLACES.XLSM" Then
    MsgBox "Someone has made a copy of the file, you may not enter data here.", vbOKOnly, "Invalid file"
    Application.Quit
End If
End Sub