martinequilibrium
New Member
- Joined
- Feb 26, 2016
- Messages
- 13
Dear Users,
I'm trying to run the code here:
Stack Overflow
When you press a button it checks the active worksheet and tries to run all the code in the worksheet object in Microsof Excel Objects that has the same name.
I tried running it but I get a Subscript Out of Range Error.
Thanks
I'm trying to run the code here:
Stack Overflow
When you press a button it checks the active worksheet and tries to run all the code in the worksheet object in Microsof Excel Objects that has the same name.
I tried running it but I get a Subscript Out of Range Error.
Thanks
Code:
'~~> Code adapted from http://www.cpearson.com/excel/vbe.aspx
Sub CallModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long, NumLines As Long
Dim ProcName As String
Dim ProcKind As VBIDE.vbext_ProcKind
Dim MyAr() As String
Dim n As Long
Dim ModuleName As String
' Aca ponemos el module name como
ModuleName = ActiveSheet.Name
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents(ModuleName)
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CountOfDeclarationLines + 1
Do Until LineNum >= .CountOfLines
ReDim Preserve MyAr(n)
ProcName = .ProcOfLine(LineNum, ProcKind)
'~~> Store the routine names in an array
MyAr(n) = ProcName
n = n + 1
LineNum = .ProcStartLine(ProcName, ProcKind) + _
.ProcCountLines(ProcName, ProcKind) + 1
Loop
End With
'~~> This is where I am running every routine from Module1
For n = LBound(MyAr) To UBound(MyAr)
Run ModuleName & "." & MyAr(n)
Next n
End Sub
Function ProcKindString(ProcKind As VBIDE.vbext_ProcKind) As String
Select Case ProcKind
Case vbext_pk_Get
ProcKindString = "Property Get"
Case vbext_pk_Let
ProcKindString = "Property Let"
Case vbext_pk_Set
ProcKindString = "Property Set"
Case vbext_pk_Proc
ProcKindString = "Sub Or Function"
Case Else
ProcKindString = "Unknown Type: " & CStr(ProcKind)
End Select
End Function