I’ve been playing with this problem for some time, and havent figured out how to do it.
I have the same function in every worksheet (and those sheets are named like this Name=”One”, CodeName=”SheetOne” …):
const someVar as Boolean = True
Public Function myFunction() as Boolean
myFunction = someVar
End Function
Now I want it to be called from outside like this – In ThisWorkbook there is procedure “doThis()” and function “TestThis():
Sub doThis()
Dim i as Integer
For i = 1 to ThisWorkbook.Sheets.Count
If testThis(ThisWorkbook.Worksheets(i)) = True then
Debug.print "Success!"
End If
Next i
Function testThis(x As Worksheet)
If x.myFunction = True Then
testThis = True
Else
testThis = False
End If
Now I know that at this line “If x.myFunction = True” it throws error “Method or data member not found”, because i cant call that function with this reference, so I’ve tried it with VBComponent:
Sub doThis()
Dim i as Integer
For i = 1 to ThisWorkbook.Sheets.Count
If testThis(ThisWorkbook.VBProject.VBComponents(Worksheets(i).CodeName)) _
= True then
Debug.print "Success!"
End If
Next i
Function testThis(x As VBComponent)
If x.myFunction = True Then
testThis = True
Else
testThis = False
End If
But again, it throws an “Object does not support this property or method” error. Any ideas, How can I call that function from a variable, that has stored reference of any kind to that Worksheet? Any help would be appreciated.
When compiling the code, Excel checks to see if “WorkSheet” object type has a
myFunctionmethod – it is looking at the generic built-in Worksheet object, and this doesn’t include your “add on” function (and neither does the “VBComponent” type), so it throws an error.If you change your parameter type to Object (a more generic type) then it will compile…