PC Review


Reply
Thread Tools Rate Thread

Determine if Sub Exists

 
 
Paige
Guest
Posts: n/a
 
      17th Mar 2009
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"
 
Reply With Quote
 
 
 
 
RB Smissaert
Guest
Posts: n/a
 
      17th Mar 2009
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" <(E-Mail Removed)> wrote in message
news:794F27DB-1223-4A0B-8491-(E-Mail Removed)...
> 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"


 
Reply With Quote
 
Howard31
Guest
Posts: n/a
 
      17th Mar 2009
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.CodeModule.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"

 
Reply With Quote
 
Howard31
Guest
Posts: n/a
 
      17th Mar 2009
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.CodeModule.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"

 
Reply With Quote
 
Paige
Guest
Posts: n/a
 
      17th Mar 2009
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.CodeModule.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"

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to determine if typ conversion exists? Thomas Microsoft C# .NET 3 9th Apr 2010 08:34 AM
How determine if directory exists windsurferLA Microsoft Excel Programming 6 3rd Nov 2007 06:14 PM
VBA:: determine if UDF exists? George Microsoft Excel Misc 1 7th May 2007 12:57 PM
determine if value exists =?Utf-8?B?Z2VlYmVl?= Microsoft Excel Programming 2 19th Sep 2006 11:03 PM
How to determine if table exists in VBA Tony Girgenti Microsoft Access Form Coding 2 8th Dec 2003 02:38 PM


Features
 

Advertising
 

Newsgroups
 


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