【问题标题】:Unable to refresh VBA code programatically无法以编程方式刷新 VBA 代码
【发布时间】:2013-12-24 15:41:41
【问题描述】:

我有一个看起来像这样的例程:

Private Sub RefreshByName(strFileType As String)
Dim strFile As String

strFile = Dir(".\*." & strFileType, vbNormal)

Do While Len(strFile) > 0
    Dim strCompName As String
    Dim vbComp As Object
    strCompName = Left(strFile, Len(strFile) - 4)
    'Fails here
    vbComp = ActiveWorkbook.VBProject.VBComponents(strCompName)
    ActiveWorkbook.VBProject.VBComponents.Remove vbComp
    ActiveWorkbook.VBProject.VBComponents.import (strFile)
    strFile = Dir
Loop

End Sub

根据我在网上阅读的大部分内容,应该可以按名称引用 VBComponents,但它不起作用。我错过了什么?

通常我得到的错误是Run-time error 438: Object doesn't support this property or method。 - 这不是我在此特定代码变体上遇到的错误 - 而是error 91: Object variable or With block variable not set

但我分解了所有声明以查看导致问题的行的确切部分。在玩这个以获得确切的错误消息时,它突然对我有用(莫名其妙地,无论如何对我来说)我现在正试图找到正确的组合,但对于错误消息的任何帮助将不胜感激。

【问题讨论】:

  • 请具体说明,而不是一般的“它不起作用”。
  • 当你debug.print时,strCompName的值是多少?
  • @Marc,在这种情况下是帐户。这是正确的名称。
  • @DougGlancy,我扩展了我的问题。
  • vbComp = 应该是 Set vbComp =,我不确定 VBE 组件的名称是否总是与其文件名匹配。

标签: vba excel excel-2013


【解决方案1】:

你应该是not using Set

Set vbComp = ActiveWorkbook.VBProject.VBComponents(strCompName)

您可能还想从.import (strFile) 发送remove the parentheses

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多