16

I have been fighting with an Excel 2007 problem for several days now. Below is a listing of all facts I can think of that might be relevant:

  1. IDetailSheet is a class declared in the VBA project with several methods, and which throws an error in its Class Initializer so that it cannot be instantiated (making it abstract).

  2. Option Explicit is set in all modules.

  3. Ten worksheets in the VBA project implement IDetailSheet and compile cleanly (as does the entire project).

  4. CDetailSheets is a class declared in the VBA project that wraps a Collection object and exposes the Collection object as a Collection of IDetailSheet. It also exposes some additional methods to perform certain methods of IDetailSheet on all collection menmbers.

  5. In its Class initializer (called from the Workbook_ Open event handler and assigned to a global variable), CDetailSheet executes the following code to populate the private collection DetailSheets:

    Dim sht as EXCEL.WorkSheet
    For Each sht in ActiveWorkbook.Worksheets
      If TypeOf sht is IDetailSheet Then
        Dim DetailSheet as IDetailSheet
        Set DetailSheet = sht
        DetailSheets.Add DetailSheet, DetailSheet.Name
      End If
    Next sht
    
  6. In certain Ribbon call-backs the following code is run:

       If TypeOf ActiveWorkbook.ActiveSheet is IDetailSheet Then
          Dim DetailSheet as IDetailSheet
          Set DetailSheet = ActiveWorkbook.ActiveSheet
          DetailSheet.Refresh  *[correction]*
       End If
    
  7. All ActiveX controls have been removed from the Workbook, after having been identified with other stability issues (There were a few dozen originally). A Fluent Interface Ribbon has been created to replace the functionality originally associated with the ActiveX controls.

  8. There is a Hyperion add-in from the corporate template, but it is not used in this workbook.

When all is said and done, the following symptom occurs when the workbook is run:

  • Any number of instances of IDetailSheet are recognized in the CDetailSheets Initializer by TypeOf Is, from 1 (most common) to occasionally 2 or 3. Never zero, never more than 3, and most certainly never the full 10 available. (Not always the same one, though being near the front of the set seems to increase likelihood of being recognized.)
  • Whichever instances of IDetailSheet implementation are discovered in the CDetailSheets initializer (and as near as I can determine, only such instances) are also recognized by TypeOf ... Is in the Ribbon call-back.

Can anyone explain why most of the TypeOf ... Is operations are failing? Or how to fix the issue?

I have resorted to manually creating v-tables (i.e. big ugly Select Case ... End Select statements) to get the functionality working, but I actually find it rather embarrassing to have my name beside such code. Besides which, I can see that being a future maintenance nightmare.

Thinking that it might be a stale p-code issues, I went to the extent of deleting the Project.Bin file from the expanded XLSM zip, and then manually importing all the VBA code back in. No change. I also tried adding the project name to all the usages of IDetailSheet to make them miFab.IDetailSheet, but again to no avail. (miFab is the project name.)

27
  • 1
    Are Foreach and EndIf typos in your question? Aug 29, 2013 at 1:13
  • 1
    Yes, but in VBA it would be For Each and End If. Aug 29, 2013 at 1:16
  • 3
    This blog entry suggests that using Implements with Worksheet objects results in instability
    – barrowc
    Aug 29, 2013 at 5:27
  • 2
    This is certainly an interesting issue, and one I've never run into before. If there's any way you can refactor your VBA application to use encapsulation instead of inheritance, you could define a bunch of bare user-defined types or classes that don't extend/implement anything, and just declare them as a private field in each of the worksheets, with the desired properties/methods/functions to keep track of data you need. It's ugly, but it would work, probably, since the issue seems to be specifically with worksheets implementing interfaces. Aug 29, 2013 at 13:32
  • 5
    Not really...? In your case, "ten worksheets in the VBA project implement IDetailSheet". I'm not the only commenter here who thinks that specifically using inheritance/interfaces on Worksheet objects in particular is the cause of the problem. This is why I'm suggesting that you create a per-sheet plain old class module implementing IDetailSheet instead, and put those in your collection. There are a number of ways that you can code up figuring out which IDetailSheet instance belongs to which Worksheet without using TypeOf or Select ... End Select. Aug 30, 2013 at 14:12

3 Answers 3

8

There are a few ways you could cheat using CallByName. You're going to have to work around this bug one way or another.

A quick dirty example

Every sheet that starts with an implementing line should have a public GetType function. I attached the "TestSheet" sub to a button on my ribbon. It puts the returned type name in cell A1 to demonstrate the function.

Module1

'--- Start Module1 ---
Option Explicit

Public Sub TestSheet()
  Dim obj As Object
  Set obj = ActiveSheet
  ActiveSheet.[A1] = GetType(obj)
End Sub

Public Function GetType(obj As Object) As String
  Dim returnValue As String
  returnValue = TypeName(obj)
  On Error Resume Next
  returnValue = CallByName(obj, "GetType", VbMethod)
  Err.Clear
  On Error GoTo 0
  GetType = returnValue
End Function
'--- End Module1 ---

Sheet1

'--- Start Sheet1 ---
Implements Class1
Option Explicit

Public Function Class1_TestFunction()
End Function

Public Function GetType() As String
    GetType = "Class1"
End Function
'--- End Sheet1 ---
3
  • 1
    I thought VBA had a CallByNae function but I was unable to find it last week. I have it now though; thank you. Sep 4, 2013 at 2:01
  • 1
    You're welcome. I figure you can probably do all your work through the CallByName function, but that could be annoying and ugly to write. This way you can still cast the worksheet to the relevant interface, or retrieve a wrapper object if implements is causing other problems.
    – AndASM
    Sep 4, 2013 at 15:22
  • I knew it would be sufficient, and a less messy work-around than I built last week, but for some reason I couldn't find it at that time. Sep 5, 2013 at 0:12
3

I found this question after posting my own similar issue as TypeOf fails to work with Excel workbook's ActiveSheet that implements interface

I don't have a definitive explanation, but I think I do have a workaround.

I suspect it is because [the code] is implementing an interface on Sheet1 or Chart, and is extending Sheet1/Chart1, but Sheet1 is already extending Worksheet (and Chart1 is already extending Chart).

In my testing, I am able to force VBA to return the real value of TypeOf by firstly accessing a property of the sheet. That means, doing something ugly like:

'Explicitly access ThisWorkbook.ActiveSheet.Name before using TypeOf
If TypeOf ThisWorkbook.Sheets(ThisWorkbook.ActiveSheet.Name) Is PublicInterface Then
0

If you're not trusting TypeOf, plough on and ignore errors:

Dim sht as EXCEL.WorkSheet
For Each sht in ActiveWorkbook.Worksheets
  'If TypeOf sht is IDetailSheet Then
  Dim DetailSheet As IDetailSheet
  On Error Resume Next
  Set DetailSheet = sht
  On Error GoTo 0
  If Not DetailSheet Is Nothing Then
    DetailSheets.Add DetailSheet, DetailSheet.Name
  End If
Next sht

If this doesn't work, the worksheets really aren't IDetailSheet at that time at least.

1
  • 1
    That was the very first work-around I tried; several days before writing the original post. My best guess is that there is some sort of timing issue / race condition going on in the STA. Sep 4, 2013 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.