2014-09-12 2 views
1

모듈 으로 Module1이있다 즉 내부 나는 Module1의 일부 매크로를 가지고 : 나는 Microsoft Excel에서 내 ThisWorkbook 가능한이 전체 Module1를 호출 할,엑셀 VBA에서에서 ThisWorkbook에 모듈을 호출하는 방법

Sub Macro1 
    ' Code 
End Sub 

Sub Macro2 
    ' Code 
End Sub 

을 지금

Sub CallingModule 
    **Call Module1 (I want to call in this way)** 
End Sub 

을하지만, 이것은 올바른 절차되지 않습니다 : 객체는 ThisWorkbook 내부

을 즉 요구. 모듈을 호출하는 올바른 절차를 알려주십시오.

+0

module1의 매크로를 하나씩 호출하는 방법은 무엇입니까? –

+0

사실 저는 전체 모듈을 호출하려고합니다. 가능한가? –

+2

아니요. 전체 모듈이 아닌 특정 루틴을 호출합니다. – Rory

답변

5

위에서 언급 한 Rory와 마찬가지로 전체 모듈이 아니라 특정 루틴을 호출합니다. 그러나 모든 루틴을 호출하려는 경우 매크로 1, 매크로 2, 매크로 3 등은 모듈에서 가능합니까?

YES

아래와 같은 Module1SIMPLE ROUTINES가있는 경우 다음 네,이 모듈의 모든 프로 시저를 호출 할 수 있습니다.

이제 단순히 Module2에이 코드를 붙여 Module1

Sub Sample1() 
    MsgBox "I am Sample1" 
End Sub 

Sub Sample2() 
    MsgBox "I am Sample2" 
End Sub 

Sub Sample3() 
    MsgBox "I am Sample3" 
End Sub 

Sub Sample4() 
    MsgBox "I am Sample4" 
End Sub 

enter image description here

에서의 당신이 있다고 가정 해 봅시다. 또한 Microsoft Visual Basic For Applications Extensibility xx.xx 라이브러리에 대한 참조를 설정해야합니다.

'~~> Code adapted from http://www.cpearson.com/excel/vbe.aspx 
Sub CallModule1() 
    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 

    Set VBProj = ActiveWorkbook.VBProject 
    Set VBComp = VBProj.VBComponents("Module1") 
    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 "Module1." & 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 

당신이 일상 CallModule1()을 실행

는, 다음으로 Module1에서 각각의 모든 과정은 자동으로 실행됩니다.

+1

몇 가지 절차를 실행하는 데 얼마나 훌륭하게 복잡한 방법입니까? - 좋아합니다! – citizenkong

+1

+1 OMG. 너는 사악 해! –

+0

lol ... 나는 다음을 기대한다 : P –

관련 문제