【问题标题】:Counting Type Variables in Excel vba, possible?在 Excel vba 中计算类型变量,可能吗?
【发布时间】:2021-04-22 17:54:27
【问题描述】:

目前我正在使用 excel vba。 我问自己是否有可能计算在用户定义的Type 中声明的变量,如下所示。

Public Type NameOfType
   a as string
   b as string
End Type

这里的结果是 2。

谢谢

【问题讨论】:

  • 没有。这称为反射,获取有关程序的信息。大多数情况下,VBA 不支持它。
  • 统计变量的目的是什么?我怀疑X/Y problem
  • UDT 中的字段数在运行时永远不会改变。使用2 可能是安全的,但这确实会引发 X-Y 问题的每一个标志。

标签: excel vba types vbe


【解决方案1】:

通过 VBA 可扩展性库的方法

假定以编程方式访问 VBA 项目,您可以

  • 提取其代码模块的声明头并
  • 分析相关代码行

(参考 需要“Microsoft Visual Basic for Applications Extensibility 5.3”)。

Sub CountTypeVars(ByVal StatementName As String)
    Dim VBAEditor      As VBIDE.VBE              ' VBE
    Dim curProject     As VBIDE.VBProject        ' Projekt
    Dim curComponent   As VBIDE.VBComponent      ' Modul
    Dim curCode        As VBIDE.CodeModule       ' Codeblock des Moduls
    Dim i              As Integer
    ' ========================================
    ' Get the project details in the workbook.
    ' ========================================
    Set VBAEditor = Application.VBE
    Set curProject = VBAEditor.ActiveVBProject
    
    For Each curComponent In curProject.VBComponents    ' check all MODULES
        ' Find the code module for the project (Codeblock in current component/=module).
        Set curCode = curComponent.CodeModule
      
        Dim ii As Long
        ii = curCode.CountOfDeclarationLines
        Dim DeclLines: DeclLines = Split(curCode.Lines(1, ii), vbNewLine)
        Dim cnt As Long, found As Boolean
        cnt = 0
        For i = LBound(DeclLines) To UBound(DeclLines)
            If UCase(DeclLines(i)) Like "* " & UCase(StatementName) & "*" Then
                Debug.Print "** Type Statement : ", DeclLines(i)
                Debug.Print "   Found in Module: ", curComponent.name & vbNewLine & String(50, "-")
                Debug.Print "Line #", "Count #", "Variable(s)" & vbNewLine & String(50, "-")
                found = True: i = i + 1
            End If
            If found And Not UCase(DeclLines(i)) Like "*END TYPE*" Then
                cnt = cnt + 1               ' increment variable counter
                Debug.Print "# " & i + 1, cnt, VBA.Trim(DeclLines(i))
            End If
                
        Next i
        If found Then
            Debug.Print vbNewLine & "** Counted " & cnt & " Type Variables."
            Exit For
        End If
    
    Next
    If Not found Then Debug.Print "** No occurrence of " & StatementName & " found!"
End Sub

VB 编辑器即时窗口中的示例输出

打电话,例如CountTypeVars "NameOfType"你可能会得到以下结果:


** Type Statement :         Public Type NameOfType
   Found in Module:         modExample
--------------------------------------------------
Line #        Count #       Variable(s)
--------------------------------------------------
# 4            1            a As String
# 5            2            b As String

** Counted 2 Type Variables.

警告

这种方法将Type 语句后面的每个代码行计为一个 变量;因此不会处理其他代码行,例如换行符或 cmets。

【讨论】:

  • 哇,@ T.M.非常好。对我来说,你的解决方案很有意义。也许如果在模块中的 End Type 之后,一些代码在内部循环的 Exit For 之后是必要的。如果我的理解是正确的...... @all 谢谢,我想我真的有一个 xy 问题。明天我会再次检查我的代码,如果一切正常,我会在这里发布我所做的。
  • 感谢回复。注意:Exit For 是正确的 :-) - 如果有帮助,请随时勾选绿色复选标记接受。
  • 好的,谢谢,我就是这么想的。 If found And UCase(DeclLines(i)) Like "END TYPE" Then Exit For End If 因为在 "End Type" 语句之后可能还有其他代码... 好的,谢谢。 :-)
猜你喜欢
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多