Excel VBA에서 주어진 모듈 이름의 Function 및 Sub 목록을 가져오는 방법
저는 액티브 엑셀 워크북에서 주어진 모듈 이름의 목록 기능을 조사하는 도우미 매크로를 작업하고 있습니다.예: "Module1"이라는 모듈 이름을 가지고 있습니다.이 모듈 내부에는 다음과 같은 기능 또는 하위 기능이 있습니다.
Sub Sub1()
End Sub
Sub Sub2()
End Sub
Function Func1()
End Function
Function Func2()
End Function
함수 및 하위 이름 목록을 반환할 수 있는 명령 또는 루틴이 있습니까?
여기에 칩 피어슨의 사이트 링크가 있습니다.VBE에 영향을 미치거나 VBE를 사용하는 프로그램이 필요할 때마다 이 프로그램을 사용합니다.흥미를 끌 수 있는 두 가지 섹션이 있습니다.하나는 프로젝트의 모든 모듈을 나열합니다.그리고 다른 하나는 모듈의 모든 절차를 나열합니다.도움이 되길 바랍니다.
http://www.cpearson.com/excel/vbe.aspx
사이트의 코드(VBIDE 개체 라이브러리에 참조를 추가하는 방법은 사이트를 방문하십시오.)
이 코드는 모듈 1의 모든 절차를 나열하고 셀 A1의 목록을 시작합니다.
Sub ListProcedures()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Dim NumLines As Long
Dim WS As Worksheet
Dim Rng As Range
Dim ProcName As String
Dim ProcKind As VBIDE.vbext_ProcKind
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Module1")
Set CodeMod = VBComp.CodeModule
Set WS = ActiveWorkbook.Worksheets("Sheet1")
Set Rng = WS.Range("A1")
With CodeMod
LineNum = .CountOfDeclarationLines + 1
Do Until LineNum >= .CountOfLines
ProcName = .ProcOfLine(LineNum, ProcKind)
Rng.Value = ProcName
Rng(1, 2).Value = ProcKindString(ProcKind)
LineNum = .ProcStartLine(ProcName, ProcKind) + _
.ProcCountLines(ProcName, ProcKind) + 1
Set Rng = Rng(2, 1)
Loop
End With
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
"MZ-Tools"라는 무료 도구도 있습니다.추가 기능으로 설치합니다. 코드 라인에 번호를 매기고, 표준 오류 관리 코드를 생성하고, 사용되지 않는 변수를 확인하고, 함수를 주문하고, 하위 및 ...는 매개 변수, 주석 등으로 절차 목록을 자동으로 생성하여 코드를 문서화합니다.훌륭한 도구!
String 컬렉션을 반환하는 함수를 찾는 사람들을 위해, 다음은 기타던지기의 답변에서 수정된 몇 가지 코드입니다.
'Collection of Strings of Sub names in that module
Private Function getAllProcNames(module As VBIDE.CodeModule) As Collection
Dim lineNum As Integer
Dim procName As String
Dim coll As New Collection
Dim ProcKind As VBIDE.vbext_ProcKind
With module
lineNum = .CountOfDeclarationLines + 1
Do Until lineNum >= .CountOfLines
procName = .ProcOfLine(lineNum, ProcKind)
lineNum = .ProcStartLine(procName, ProcKind) + _
.ProcCountLines(procName, ProcKind) + 1
coll.Add Item:=procName
Loop
End With
Set getAllProcNames = coll
End Function
ProcKind 변수는 그냥 버려집니다. 이것은 이름만 제공합니다.
' a bit more info for those who like me looking for help
' without Chip Pearson and many others my programming would still be at
' x=x+4
Option Explicit
'
' to list or sort procedure names
'
'
' on a spare sheet
'
Private Sub CommandButton1_Click()
Dim URA$, RaSort As Range, ModName$, VBC As VBComponent
Dim RangeStartAddress$: RangeStartAddress = "H11" ' any spare region
Set RaSort = Range(RangeStartAddress)
' sort and display needs 5 un-bordered columns so best done from spare worksheet
RaSort(0, 0).Resize(UsedRange.Rows.Count, 7).Clear
URA = UsedRange.Address ' tidy of used range
ModName = [c6]
' from cell C4 ... or whatever is needed name is needed
' OR ... to do all modules ... Skipping workbook try something like
'
'For Each VBC In ActiveWorkbook.VBProject.VBComponents
' Range("G11:N" & UsedRange.Rows.Count).Clear
' URA = UsedRange.Address
'Set RaSort = Range("h11")
'If Not (VBC.Name Like "Workbook") Then
' SortSUBLGFUN VBC.Name, RaSort
'End If
' Next VBC
SortSUBLGFUN ModName, RaSort
End Sub
'
' in a module
'
' sort the procedure names for a module
' Reference to VBE .. Microsoft Visual Basic for Applications Extensibility
' RaSort as some spare Range CurrentRegion
'
Sub SortSUBLGFUN(ComponentName$, RaSort As Range)
Dim LineI%, PBLI&, RowI&, RowOut&, LineStr$
Dim PLSG As vbext_ProcKind ' 0 Fun or Sub 1 Let 2 Set 3 Get
Dim ProcName$
Dim StartLineI&, CountLinesI&, LinesOfProc$
With ActiveWorkbook.VBProject.VBComponents(ComponentName).CodeModule
LineI = .CountOfDeclarationLines + 1
While LineI < .CountOfLines
PLSG = 0
While PLSG < 3 And LineI < .CountOfLines ' look for all types
On Error GoTo LookMore ' msny may not exist
ProcName = .ProcOfLine(LineI, PLSG)
CountLinesI = .ProcCountLines(ProcName, PLSG)
StartLineI = .ProcStartLine(ProcName, PLSG)
RowOut = RowOut + 1
RaSort(RowOut, 1) = ProcName
RaSort(RowOut, 2) = PLSG
RaSort(RowOut, 3) = StartLineI
RaSort(RowOut, 4) = CountLinesI
' the procedure can have blanks or comment lines at the top
' so start line is not always the Procedure body line
' the ProcBodyLine may be extended for over about 20 lines
' using the line-continuation char " _"
' so it looks a bit complex to find the actual line
PBLI = .ProcBodyLine(ProcName, PLSG)
LineStr = .Lines(PBLI, 1)
While Right(LineStr, 2) = " _" ' if extended get the other lines
PBLI = PBLI + 1
LineStr = Left(LineStr, Len(LineStr) - 2) & " " & .Lines(PBLI, 1)
Wend
RaSort(RowOut, 5) = LineStr
LineI = StartLineI + CountLinesI + 1
If LineI > .CountOfLines Then PLSG = 14 ' > 3
LookMore:
On Error GoTo 0
PLSG = PLSG + 1
Wend
LineI = LineI + 1
Wend
Set RaSort = RaSort.CurrentRegion
RaSort.Sort RaSort(1, 1), xlAscending
'
'bring each to the top from Z to A results in sorted alphabetically
'
For RowI = RaSort.Rows.Count To 1 Step -1
ProcName = RaSort(RowI, 1)
PLSG = RaSort(RowI, 2)
'
' since they have moved need to refind them before moving to top
'
CountLinesI = .ProcCountLines(ProcName, PLSG)
StartLineI = .ProcStartLine(ProcName, PLSG)
LinesOfProc = .Lines(StartLineI, CountLinesI)
.DeleteLines StartLineI, CountLinesI
.InsertLines .CountOfDeclarationLines + 1, LinesOfProc
Next RowI
End With
End Sub
'
' you may find the two below of interest
'
Sub TabsAscending()
Dim I&, J&
For I = 1 To Application.Sheets.Count
For J = 1 To Application.Sheets.Count - 1
If UCase$(Application.Sheets(J).Name) > UCase$(Application.Sheets(J + 1).Name) then
Sheets(J).Move after:=Sheets(J + 1)
End If
Next J
Next I
End Sub
Sub ResetCodeNames(WkWb As Workbook)
'Changes the codename conventional name gets rid of Sheet3 Sheet7 where they have been given a name
Dim VarItem As VBIDE.VBComponent
For Each VarItem In WkWb.VBProject.VBComponents
'Type 100 is a worksheet
If VarItem.Type = 100 And VarItem.Name <> "ThisWorkbook" Then
VarItem.Name = VarItem.Properties("Name").Value
End If
Next
End Sub
' hope it helps others
언급URL : https://stackoverflow.com/questions/2630872/how-to-get-the-list-of-function-and-sub-of-a-given-module-name-in-excel-vba
'programing' 카테고리의 다른 글
"도..."가 있습니까?파이썬에서 "까지"? (0) | 2023.06.08 |
---|---|
Python을 사용하여 Excel에 수식 쓰기 (0) | 2023.06.08 |
Firebase에서 배열 유형 문서 항목에 타임스탬프를 할당하는 방법 (0) | 2023.06.08 |
pyproject.toml 파일은 무엇을 위한 것입니까? (0) | 2023.06.08 |
입력을 테스트하고 있습니다.효소의 초점() (0) | 2023.06.08 |