Convert Subroutine to Function?

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
417
Office Version
  1. 2007
Platform
  1. Windows
Hello,

As previously stated, I must first preface this with I am not a programmer, but I try.
I have been working on this for awhile. I posted a similar question on a different site but have not received an answer.

I’m looking for an Excel function to delete a subroutine(s). I found a sub on-line that does this (I wish I could find it again and give the author credit). However, since it is written to delete only one specific procedure and I have more than one I may wish to delete, I would have to copy/edit/rename this sub numerous times. I believe a function would make this process easier. Therefore, my question – Is it possible to convert the Subroutine into a Function?

Here is the existing Subroutine I would like converted to a function:
VBA Code:
Sub DeleteProcedureFromModule()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim StartLine As Long
        Dim NumLines As Long
        Dim ProcName As String
  
        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("Module1")
        Set CodeMod = VBComp.CodeModule
 
        ProcName = "DeleteThisProc”
  
        With CodeMod
            StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
            NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
            .DeleteLines StartLine:=StartLine, Count:=NumLines
        End With
End Sub

_______________________________________________________

NOW, here is a bit more info that may help –

What I had in mind is I’d like some function to delete a subroutine based on the parameter entered after the function. Let’s say the function is called DeleteEm() and the subroutine I want to delete is called TryMe. Then the syntax would be something like: DeleteEm(“TryMe”).

I also have a function I found on-line that checks if a procedure exists:
Code:
Function ProcExists(ProcName) As Boolean

Dim MyModule As Object
Dim MyModuleName As String
Dim MyLine As Long
Dim FoundIT As Boolean

On Error GoTo WTF

If LCase(Left(ProcName, 2)) = "tb" Then

'This next line is the doorstop:

For Each MMN In AddIns("JGM ToolBox.xla").VBProject.VBComponents
     MyModuleName = MMN.Name
     Set MyModule = AddIns("JGM ToolBox.xla").VBProject.VBComponents(MyModuleName).CodeModule
     MyLine = MyModule.ProcStartLine(ProcName, vbext_pk_Proc)
     If MyLine <> 0 Then FoundIT = True Else FoundIT = False
     If FoundIT Then Exit For
     Next
Else
     For Each MMN In ActiveWorkbook.VBProject.VBComponents
     MyModuleName = MMN.Name
     Set MyModule = ActiveWorkbook.VBProject.VBComponents(MyModuleName).CodeModule
     MyLine = MyModule.ProcStartLine(ProcName, vbext_pk_Proc)
     If MyLine <> 0 Then FoundIT = True Else FoundIT = False
     If FoundIT Then Exit For
     Next
End If

ProcExists = FoundIT

Exit Function


Here is my TEST routine to do something if the procedure TryMe exists (i.e., delete TryMe if it exists):
Code:
Sub Test1()
If ProcExists("TryMe") = True Then
     MsgBox "Yup – it’s there"
     DeleteEm(“TryMe”)
Else
     MsgBox "NOPE – not there"
End If
End Sub

Any suggestions would be appreciated.

Thanks for viewing,
Steve K.
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Rich (BB code):
'You can pass arguments to your Sub

Sub LikeThis()

    On Error Resume Next
    Call DeleteProcedureFromModule("Module1", "DeleteThisProc")
    Call DeleteProcedureFromModule("Module1", "DeleteAnotherProc")
    On Error GoTo 0

End Sub
Sub DeleteProcedureFromModule(ModuleName As String, ProcName As String)
        
    '...
    
    'Delete this line
    Dim ProcName As String
    
    '...
    
    Set VBComp = VBProj.VBComponents(ModuleName)
    
    'Delete this line
    ProcName = "DeleteThisProc"
    
    '...

End Sub
 
Upvote 0
Thank you Stephen for your quick response.

I’m confused (nothing new). I’m not quite sure how I go about running these two subs. I assume I am to input the sub name I want to delete (say TryMe). I don’t mean to be too demanding here but could you shed a bit more direction here.

Where do I put the name of the Sub name I want to delete – in the LikeThis, DeleteProcedureFromModule, or both?

What I was trying is to do is place my test sub (TryMe) in:
Call DeleteProcedureFromModule("Module1", "TryMe")

Do I have to add my ModuleName (i.e., Module1) or is that covered under the line
Call DeleteProcedureFromModule("Module1", "DeleteThisProc")style='font-size:11.0pt;font-family:"Arial","sans-serif";color:#141414'>


Sorry for being so uninformed,
Steve
 
Upvote 0
Try it like this.

(The On Error Resume Next means you don't need to test the existence of the Sub before trying to delete it.)

VBA Code:
Sub Main()

    'Do stuff

    'Delete unwanted Subs
    On Error Resume Next
    Call DeleteProcedureFromModule("Module1", "TryMe")
    Call DeleteProcedureFromModule("Module1", "SomeProcThatDoesntExist")
    On Error GoTo 0

    'Do stuff
   
End Sub
Sub DeleteProcedureFromModule(ModuleName As String, ProcName As String)
       
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim StartLine As Long
    Dim NumLines As Long
   
    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents(ModuleName)
    Set CodeMod = VBComp.CodeModule
       
    With CodeMod
        StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
        NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
        .DeleteLines StartLine:=StartLine, Count:=NumLines
    End With

End Sub
 
Upvote 0
Solution
Try it like this.

(The On Error Resume Next means you don't need to test the existence of the Sub before trying to delete it.)

VBA Code:
Sub Main()

    'Do stuff

    'Delete unwanted Subs
    On Error Resume Next
    Call DeleteProcedureFromModule("Module1", "TryMe")
    Call DeleteProcedureFromModule("Module1", "SomeProcThatDoesntExist")
    On Error GoTo 0

    'Do stuff
  
End Sub
Sub DeleteProcedureFromModule(ModuleName As String, ProcName As String)
      
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim StartLine As Long
    Dim NumLines As Long
  
    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents(ModuleName)
    Set CodeMod = VBComp.CodeModule
      
    With CodeMod
        StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
        NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
        .DeleteLines StartLine:=StartLine, Count:=NumLines
    End With

End Sub

Excellent - Thank you very much Stephen. This is what I had in mind.
Again, much appreciated. . .
 
Upvote 0
Once again:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Once again:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
Rory,

I understand cross-posting is frowned upon. However, I did mention my issue was posted elsewhere. I did not simply include a link but rather attempted to explain in detail my issue. The reason for the cross-post is just that I feel the more eyes on a subject the better chance of receiving an answer, particularly when the question is quite unique and if the previous post in the “other” forum is not answered.

I really hope I did not offend anyone here. That surely was not my intent.

Thanks again,
Steve K.
 
Upvote 0
I did not simply include a link but rather attempted to explain in detail my issue
Unfortunately, the rules here - and elsewhere - require a link, for reasons which are made clear within the rules. As long as you provide the link(s), there isn't a problem.
 
Upvote 0
Unfortunately, the rules here - and elsewhere - require a link, for reasons which are made clear within the rules. As long as you provide the link(s), there isn't a problem.
OKAY, I did not realize that. I will most certainly do so in future.

Again, thank you. . .
Steve
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top