ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Determine if Sub Exists (https://www.excelbanter.com/excel-programming/425712-determine-if-sub-exists.html)

Paige

Determine if Sub Exists
 
Is there a way to determine if a sub exists? I have code in an add-in that
calls a sub in the active workbook per the example below. How do I determine
if that sub exists in the activeworkbook or not, to avoid an error?

WBName = ActiveWorkbook.Name
Application.Run "'" & WBName & "'!RemoveSN"

RB Smissaert

Determine if Sub Exists
 
Just add an error handler:

Sub test()

On Error GoTo ERROROUT

Application.Run "xxx"

Exit Sub

ERROROUT:
MsgBox Err.Description 'or do whatever is suitable

End Sub


RBS


"Paige" wrote in message
...
Is there a way to determine if a sub exists? I have code in an add-in
that
calls a sub in the active workbook per the example below. How do I
determine
if that sub exists in the activeworkbook or not, to avoid an error?

WBName = ActiveWorkbook.Name
Application.Run "'" & WBName & "'!RemoveSN"



Howard31

Determine if Sub Exists
 
Hi,
Try the following:

If ProcExists("RemoveSN") Then Application.Run "'" & WBName & "'!RemoveSN"

Function ProcExists(ProcName As String) As Boolean
Dim i
On Error Resume Next
MsgBox
ThisWorkbook.VBProject.VBE.ActiveCodePane.CodeModu le.ProcBodyLine(ProcName,
vbext_pk_Proc)

If Err.Number < 0 Then
ProcExists = False
Else
ProcExists = True
End If
End Function
--
A. Ch. Eirinberg


"Paige" wrote:

Is there a way to determine if a sub exists? I have code in an add-in that
calls a sub in the active workbook per the example below. How do I determine
if that sub exists in the activeworkbook or not, to avoid an error?

WBName = ActiveWorkbook.Name
Application.Run "'" & WBName & "'!RemoveSN"


Howard31

Determine if Sub Exists
 
Actually there is no use for a MsgBox instead use the i variable as follows:

If ProcExists("RemoveSN") Then Application.Run "'" & WBName & "'!RemoveSN"

Function ProcExists(ProcName As String) As Boolean
Dim i
On Error Resume Next
i =
ThisWorkbook.VBProject.VBE.ActiveCodePane.CodeModu le.ProcBodyLine(ProcName,
vbext_pk_Proc)

If Err.Number < 0 Then
ProcExists = False
Else
ProcExists = True
End If
End Function

--
A. Ch. Eirinberg


"Paige" wrote:

Is there a way to determine if a sub exists? I have code in an add-in that
calls a sub in the active workbook per the example below. How do I determine
if that sub exists in the activeworkbook or not, to avoid an error?

WBName = ActiveWorkbook.Name
Application.Run "'" & WBName & "'!RemoveSN"


Paige

Determine if Sub Exists
 
Thanks, RB and Howard, for both great ideas!

"Howard31" wrote:

Actually there is no use for a MsgBox instead use the i variable as follows:

If ProcExists("RemoveSN") Then Application.Run "'" & WBName & "'!RemoveSN"

Function ProcExists(ProcName As String) As Boolean
Dim i
On Error Resume Next
i =
ThisWorkbook.VBProject.VBE.ActiveCodePane.CodeModu le.ProcBodyLine(ProcName,
vbext_pk_Proc)

If Err.Number < 0 Then
ProcExists = False
Else
ProcExists = True
End If
End Function

--
A. Ch. Eirinberg


"Paige" wrote:

Is there a way to determine if a sub exists? I have code in an add-in that
calls a sub in the active workbook per the example below. How do I determine
if that sub exists in the activeworkbook or not, to avoid an error?

WBName = ActiveWorkbook.Name
Application.Run "'" & WBName & "'!RemoveSN"



All times are GMT +1. The time now is 01:09 AM.

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