无法以编程方式刷新VBA代码 [英] Unable to refresh VBA code programatically

查看:118
本文介绍了无法以编程方式刷新VBA代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例程,如下所示:

I have a routine that looks like this:

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引用名字,但它不工作。我缺少什么?

According to most of what I read online, it should be possible to reference the VBComponents by name, but it doesn't work. What am I missing?

通常我得到的错误是运行时错误438:对象不支持此属性或方法。 - 这不是我对代码的这个特定变体的错误 - 而是一个错误91:对象变量或块变量未设置

Generally the error I get is Run-time error 438: Object doesn't support this property or method. - that isn't the error I get on this specific variant of the code - rather this is an error 91: Object variable or With block variable not set.

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

But I broke up all the declarations to see the exact part of the line that caused the problem. In playing with this to get the exact error message, It suddenly worked on me (inexplicably, to me anyway) I'm trying to find the correct combination now, but any help with the error messages would be appreciated.

推荐答案

您是不使用设置 ,您应该:

You are not using Set when you should:

Set vbComp = ActiveWorkbook.VBProject.VBComponents(strCompName)

您可能还想要 .import(strFile)中删除​​括号

You might also want to remove the parentheses from .import (strFile).

这篇关于无法以编程方式刷新VBA代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆