title: VBA custom procedure/function in Worksheet called outside its module
body: <p>I've been playing with this problem for some time, and havent figured out how to do it.
  <strong>I have the same function in every worksheet</strong> (and those sheets are named like this Name="One", CodeName="SheetOne" ...):</p>
  
  <pre><code>const someVar as Boolean = True
  
  Public Function myFunction() as Boolean
  myFunction = someVar
  End Function
  </code></pre>
  
  <p>Now I want it to be called from outside like this - In ThisWorkbook there is procedure  "doThis()" and function "TestThis():</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Now I know that at this line "<strong>If x.myFunction = True</strong>" 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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>