Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Testing for Valid Macro Name

Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Testing for Valid Macro Name

You would have to write code to read all the information in the modules and
parse out the macro names.

A start is he

http://www.cpearson.com/excel/vbe.htm

Or you might try something like this:

Sub AAA()
s = "MyMacro"
On Error Resume Next
Application.Run s
If Err.Number < 0 Then
MsgBox s & " is not a macro name or there is an error in the macro"
Else
MsgBox "OK"
End If
On Error goto 0

End Sub

--
Regards,
Tom Ogilvy


"Sprinks" wrote:

Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Testing for Valid Macro Name

Tom,

Thanks for your response.

"Tom Ogilvy" wrote:

You would have to write code to read all the information in the modules and
parse out the macro names.

A start is he

http://www.cpearson.com/excel/vbe.htm

Or you might try something like this:

Sub AAA()
s = "MyMacro"
On Error Resume Next
Application.Run s
If Err.Number < 0 Then
MsgBox s & " is not a macro name or there is an error in the macro"
Else
MsgBox "OK"
End If
On Error goto 0

End Sub

--
Regards,
Tom Ogilvy


"Sprinks" wrote:

Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default Testing for Valid Macro Name

Hello Sprinks
Here's one way I use (please apologise for its beeing in french but you will
easily find translation I am sure)
HTH
Cordially
Pascal

Sub Recherche_Macro()
'Ajouter une référence à
'Microsoft VisualBasic For Application Extensibility 5.3 (Menu
Outils|Références de l'éditeur VBA)

If ActiveWorkbook.Name = ThisWorkbook.Name Then
MsgBox "Le classeur actif ne doit pas contenir la macro Recherche_Macro"
_
, vbInformation + vbOKOnly, "Arrêt"
Exit Sub
End If
Dim Saisie, LeNom As String
Saisie = InputBox("Saisir le nom de la macro cherchée" & vbLf & "en
respectant les majuscules / minuscules")
If Saisie = "" Then Exit Sub
LeNom = "Sub " & Saisie
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim RechercheLaMacro
Dim i As Integer, y As Integer
For i = 1 To ActiveWorkbook.VBProject.VBComponents.Count
Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents(i).CodeModul e
With VBCodeMod
For y = 1 To .CountOfLines
StartLine = .CountOfDeclarationLines + 1
RechercheLaMacro = .Find(LeNom, StartLine, 1, .CountOfLines, -1,
True, True, False)
Next
End With
Next
If RechercheLaMacro = True Then
GoTo trouve
Else
GoTo pastrouve
End If

Exit Sub
trouve:
MsgBox "La macro " & Saisie & " est bien dans le classeur " &
ActiveWorkbook.Name
Exit Sub
pastrouve:
MsgBox "La macro " & Saisie & " n'est pas dans le classeur " &
ActiveWorkbook.Name
Exit Sub
End Sub


"Sprinks" a écrit dans le message de
news: ...
Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Testing for Valid Macro Name

Papou,

Parce que on parle francais ici, je n'avais pas change le Sub. La saveur est
plus delicieuse en francais. ;) Mais, malheuresement, il marche trop
lentement pour mon objet.

En fin, j'ai decide de traiter l'erreur qui se presente avec un "Resume
Next" declaration. Simple!

Merci encore.

Sprinks

"papou" wrote:

Hello Sprinks
Here's one way I use (please apologise for its beeing in french but you will
easily find translation I am sure)
HTH
Cordially
Pascal

Sub Recherche_Macro()
'Ajouter une référence Ã*
'Microsoft VisualBasic For Application Extensibility 5.3 (Menu
Outils|Références de l'éditeur VBA)

If ActiveWorkbook.Name = ThisWorkbook.Name Then
MsgBox "Le classeur actif ne doit pas contenir la macro Recherche_Macro"
_
, vbInformation + vbOKOnly, "Arrêt"
Exit Sub
End If
Dim Saisie, LeNom As String
Saisie = InputBox("Saisir le nom de la macro cherchée" & vbLf & "en
respectant les majuscules / minuscules")
If Saisie = "" Then Exit Sub
LeNom = "Sub " & Saisie
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim RechercheLaMacro
Dim i As Integer, y As Integer
For i = 1 To ActiveWorkbook.VBProject.VBComponents.Count
Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents(i).CodeModul e
With VBCodeMod
For y = 1 To .CountOfLines
StartLine = .CountOfDeclarationLines + 1
RechercheLaMacro = .Find(LeNom, StartLine, 1, .CountOfLines, -1,
True, True, False)
Next
End With
Next
If RechercheLaMacro = True Then
GoTo trouve
Else
GoTo pastrouve
End If

Exit Sub
trouve:
MsgBox "La macro " & Saisie & " est bien dans le classeur " &
ActiveWorkbook.Name
Exit Sub
pastrouve:
MsgBox "La macro " & Saisie & " n'est pas dans le classeur " &
ActiveWorkbook.Name
Exit Sub
End Sub


"Sprinks" a écrit dans le message de
news: ...
Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Testing for Valid Macro Name

Try a function like


Function IsValidProcName(ProcName As String) As String
' reference to "Microsoft VBScript Regular Expressions 5.5
Dim Pattern As String
Dim RExp As VBScript_RegExp_55.RegExp

Set RExp = New VBScript_RegExp_55.RegExp
Pattern = "^[A-Za-z]+[A-Za-z0-9_]*$"
RExp.Pattern = Pattern

IsValidProcName = RExp.Test(ProcName)
End Function



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Sprinks" wrote in message
...
Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Testing for Valid Macro Name

Chip,

Thank you for your response.

I'm afraid I don't understand your code, but in any case, it returns True
whether the string is a valid name or not.

??

Sprinks

"Chip Pearson" wrote:

Try a function like


Function IsValidProcName(ProcName As String) As String
' reference to "Microsoft VBScript Regular Expressions 5.5
Dim Pattern As String
Dim RExp As VBScript_RegExp_55.RegExp

Set RExp = New VBScript_RegExp_55.RegExp
Pattern = "^[A-Za-z]+[A-Za-z0-9_]*$"
RExp.Pattern = Pattern

IsValidProcName = RExp.Test(ProcName)
End Function



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Sprinks" wrote in message
...
Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Testing for Valid Macro Name

I'm afraid I don't understand your code, but in any case, it returns True
whether the string is a valid name or not.


I thought you meant a syntactically valid procedure name, not necessarily
the name of an existing procedure. My mistake. The code I posted only tests
whether the procedure name is allowable by VBA syntax, not whether the
procedure actually exists.

Regular Expressions are a method of testing whether a given text value
matches a pattern. The pattern I used,

^[A-Za-z]+[A-Za-z0-9_]*$

tests for a beginning of string (^), followed by one or more charcters in
the range A-Z (upper or lower case), followed be zero or more characters in
the range A-Z a-z 0-9 or an underscore, followed by and end of string ($).


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Sprinks" wrote in message
...
Chip,

Thank you for your response.

I'm afraid I don't understand your code, but in any case, it returns True
whether the string is a valid name or not.

??

Sprinks

"Chip Pearson" wrote:

Try a function like


Function IsValidProcName(ProcName As String) As String
' reference to "Microsoft VBScript Regular Expressions 5.5
Dim Pattern As String
Dim RExp As VBScript_RegExp_55.RegExp

Set RExp = New VBScript_RegExp_55.RegExp
Pattern = "^[A-Za-z]+[A-Za-z0-9_]*$"
RExp.Pattern = Pattern

IsValidProcName = RExp.Test(ProcName)
End Function



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Sprinks" wrote in message
...
Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Testing for Valid Macro Name

Chip,

Thank you for your explanation. I will store your post for when it might be
applicable--that could be a powerful tool.

Sprinks

"Chip Pearson" wrote:

I'm afraid I don't understand your code, but in any case, it returns True
whether the string is a valid name or not.


I thought you meant a syntactically valid procedure name, not necessarily
the name of an existing procedure. My mistake. The code I posted only tests
whether the procedure name is allowable by VBA syntax, not whether the
procedure actually exists.

Regular Expressions are a method of testing whether a given text value
matches a pattern. The pattern I used,

^[A-Za-z]+[A-Za-z0-9_]*$

tests for a beginning of string (^), followed by one or more charcters in
the range A-Z (upper or lower case), followed be zero or more characters in
the range A-Z a-z 0-9 or an underscore, followed by and end of string ($).


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Sprinks" wrote in message
...
Chip,

Thank you for your response.

I'm afraid I don't understand your code, but in any case, it returns True
whether the string is a valid name or not.

??

Sprinks

"Chip Pearson" wrote:

Try a function like


Function IsValidProcName(ProcName As String) As String
' reference to "Microsoft VBScript Regular Expressions 5.5
Dim Pattern As String
Dim RExp As VBScript_RegExp_55.RegExp

Set RExp = New VBScript_RegExp_55.RegExp
Pattern = "^[A-Za-z]+[A-Za-z0-9_]*$"
RExp.Pattern = Pattern

IsValidProcName = RExp.Test(ProcName)
End Function



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Sprinks" wrote in message
...
Can anyone tell me how to test if a given string is a valid macro name?

Thank you.

Sprinks


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing Spreadsheet Cells while in a Macro Amaross Excel Discussion (Misc queries) 2 May 2nd 06 09:03 PM
Testing Print Macro Brian Matlack[_31_] Excel Programming 3 November 22nd 05 02:14 PM
Run only part of a macro for testing Josh O. Excel Programming 4 May 19th 05 04:49 PM
Testing in a macro for bad range name? Don Wiss Excel Programming 1 November 3rd 04 02:26 AM
Macro Date Testing BKBK Excel Programming 0 October 28th 04 05:42 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"