I've started reading Professional Excel Development, which is a great book/pdf, and came across a suggestion to use codenames rather than worksheet.name to refer to sheets.
One of my existing protect procedures had an optional parameter for passing the sheet name, so it would protect just the one sheet instead of all sheets. So, to change this, I just had it accept a worksheet, instead of a string, parameter, and passed it a codename, like this:
Public Sub ProtectSheet(Optional wksCurr As Worksheet)
Dim strPW As String
strPW = "test"
If wksCurr Is Nothing Then
For Each wksCurr In ThisWorkbook.Worksheets
wksCurr.Protect (strPW)
Next wksCurr
Else
wksCurr.Protect (strPW)
End If
Set wksCurr = Nothing
End Sub
Now, my problem. When I call this with a codename-referenced object:
call protectsheet(sheet1)
it creates a ghost sheet in the VBA project. I tested this in a new sheet, by just creating a new module, pasting in the above code, and calling it from the immediate window. The sheet doesn't show up in the IDE right away, but if you close the VBA IDE and re-open it, you will see a new Excel Object:
Sheet4 (Sheet1)
in the object explorer, but not in the Excel GUI.
Has anyone seen this behavior before? I'm using Excel 2007, upgrading to 2010 soon.
I know I can regress to passing sheet names, I just hate the idea of going backward...
One of my existing protect procedures had an optional parameter for passing the sheet name, so it would protect just the one sheet instead of all sheets. So, to change this, I just had it accept a worksheet, instead of a string, parameter, and passed it a codename, like this:
Public Sub ProtectSheet(Optional wksCurr As Worksheet)
Dim strPW As String
strPW = "test"
If wksCurr Is Nothing Then
For Each wksCurr In ThisWorkbook.Worksheets
wksCurr.Protect (strPW)
Next wksCurr
Else
wksCurr.Protect (strPW)
End If
Set wksCurr = Nothing
End Sub
Now, my problem. When I call this with a codename-referenced object:
call protectsheet(sheet1)
it creates a ghost sheet in the VBA project. I tested this in a new sheet, by just creating a new module, pasting in the above code, and calling it from the immediate window. The sheet doesn't show up in the IDE right away, but if you close the VBA IDE and re-open it, you will see a new Excel Object:
Sheet4 (Sheet1)
in the object explorer, but not in the Excel GUI.
Has anyone seen this behavior before? I'm using Excel 2007, upgrading to 2010 soon.
I know I can regress to passing sheet names, I just hate the idea of going backward...