ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   VBA:: determine if UDF exists? (https://www.excelbanter.com/excel-discussion-misc-queries/141798-vba-determine-if-udf-exists.html)

George[_3_]

VBA:: determine if UDF exists?
 
In a workbook w/ multiple sheets, w/ a loop thus ...

dim mySheet as worksheet
For Each mySheet In ActiveWorkbook.Sheets
mySheet.Activate
Worksheets(mySheet.Name).myUDF
Next mySheet

Can I test whether 'myUDF' exists in each mySheet?

I can solve my problem using 'On Error GoTo ...', or by creating a stub
in each sheet; I'm just wondering if there's a more direct way to get
there.

Thanks,
George

Dave Peterson

VBA:: determine if UDF exists?
 
I think you'd have to know something about what the UDF needs passed to it, too.

I put this in a few worksheet modules:

Option Explicit
Function myUDF(SomeVar As Variant) As Variant
'do lots of work here
myUDF = 999
End Function


Then I could use something like this to look for it:

Option Explicit
Sub testme()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
On Error Resume Next
Application.Run wks.CodeName & ".myUDF", "aa"
If Err.Number < 0 Then
MsgBox "not found in: " & wks.CodeName & "--" & wks.Name
Err.Clear
Else
MsgBox "Found in: " & wks.CodeName & "--" & wks.Name
End If
On Error GoTo 0
Next wks
End Sub

But for the most part, if I'm gonna use the same UDF in various spots, I'll put
it in a general module and just have the other code call that single UDF.



George wrote:

In a workbook w/ multiple sheets, w/ a loop thus ...

dim mySheet as worksheet
For Each mySheet In ActiveWorkbook.Sheets
mySheet.Activate
Worksheets(mySheet.Name).myUDF
Next mySheet

Can I test whether 'myUDF' exists in each mySheet?

I can solve my problem using 'On Error GoTo ...', or by creating a stub
in each sheet; I'm just wondering if there's a more direct way to get
there.

Thanks,
George


--

Dave Peterson


All times are GMT +1. The time now is 07:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com