- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个 Excel 文件,父文件和子文件,其中子文件包含父函数使用的函数库。出于版本控制的目的,我将它们保存在同一个文件夹中,并在完全相同的位置复制和重命名该文件夹以跟踪我的版本。我还希望动态更新引用,以便当我移动到新版本时,父级始终指向同一位置的子级。
因此,为了实现这一点,我在父级中实现了两个例程。
一、在ThisWorkbook中我使用了Workbook_Open子:
Private Sub Workbook_Open()
'Force the location of the shared library to the current project folder irrespective where the project is located
Call reloadSharedLibrary
End Sub
Public librName As Variant
Public isRefReloaded As Boolean
Sub reloadSharedLibrary()
isRefReloaded = True
Dim VBAEditor As VBIDE.VBE
Dim vbProj As VBIDE.VBProject
Dim chkRef As VBIDE.Reference
Dim BoolExists As Boolean
Dim librPath As String
Set VBAEditor = Application.VBE
Set vbProj = ActiveWorkbook.VBProject
librName = "lib_emtm"
librPath = Application.ActiveWorkbook.Path & "\lib.xlsm"
' delete any shared lib (if exists)
For Each chkRef In vbProj.References
If chkRef.Name = librName Then
vbProj.References.Remove chkRef
BoolExists = True
End If
Next
' you can only add it to VBAProject only after you quit the above loop
On Error Resume Next
vbProj.References.AddFromFile librPath
If Err.Number <> 0 Then
MsgBox "FATAR ERROR: Cannot find shared library file in project root": End
End If
Set vbProj = Nothing
Set VBAEditor = Nothing
End Sub
最佳答案
问题在于,当 VBA 项目加载文档及其引用时,它会为它们分配一个名称,即 lib_emtm
。在你的情况下。当您取消选中对它的引用时,该引用将从 VBA 项目中删除,但项目编辑器会将名称保留在其缓存中。此名称将保留在缓存中,直到您关闭工作簿并重新打开它。
您可以在项目引用菜单中验证这一点:您将看到即使您取消选中引用,库的名称 lib_emtm
仍然会出现在那里。
然后当您尝试添加对“其他”子工作簿(同一文件夹中的那个)的引用时,编辑器会发现名称为lib_emtm
这与缓存中的相同,因此不会打开新文档并对其进行解析,而是使用缓存的版本,即旧版本!
如果您关闭然后重新打开应用程序,该库的名称将从缓存中消失,因此您可以安装正确的版本。完整地说,此模式仅在引用其他工作簿时出现,而不是在常规 DLL
中出现。安装在系统上。
我试过但找不到删除 Cached library
的 VBA 方法在重新安装之前从编辑器的缓存中获取。如果有人找到一种方法,它将完成解决方案。因此,目前我们必须先关闭该文档,然后再重新打开它并安装 lib。这个过程可能是自动化的,但我建议使用一个提示用户的解决方案。
' Module ThisWorkbook
Option Explicit
Private Sub Workbook_Open()
'Force the location of the shared library to the current project folder irrespective where the project is located
Dim check As Boolean: check = checkSharedLibrary
If check Then Exit Sub
Dim prompt
prompt = MsgBox("The installed lib_emtm library was uninstalled because it was not the correct version." & vbCrLf & _
"If you click Ok, document will close and the correct version will be automatically installed when you reopen it." & vbCrLf & _
"If you click Cancel, library will not be available in this session but will be installed next time you open the document", vbOKCancel)
If prompt = vbOK Then ThisWorkbook.Close True
End Sub
' Regular module
Option Explicit
Private librName As String, librpath As String
' if correct version already installed (correct path) return true
' if library installed with incorrect version, uninstall it and return false
' if library not installed, install it and return true
Public Function checkSharedLibrary() As Boolean
librName = "lib_emtm"
librpath = ThisWorkbook.Path & "\lib_emtm.xlsm"
Dim chkRef As VBIDE.Reference
For Each chkRef In ThisWorkbook.VBProject.References
If chkRef.name = librName Then Exit For
Next
If chkRef Is Nothing Then
install_emtm
checkSharedLibrary = True
ElseIf Left(chkRef.FullPath, InStrRev(chkRef.FullPath, "\") - 1) = ThisWorkbook.Path Then
checkSharedLibrary = True ' we have the correct version
Else
ThisWorkbook.VBProject.References.Remove chkRef ' return false
End If
End Function
Private Sub install_emtm()
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromFile(librpath)
If Err.Number <> 0 Then MsgBox "FATAR ERROR: Could not install lib_emtm:" & vbCrLf & Err.Description & vbCrLf & vbCrLf & _
"Please verify that the library's file is present in the same folder or try a manual install"
End Sub
关于excel - 无法刷新对外部 xlsm 文件的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732474/
我准备了一个带有 Visual Basic 代码的 excel 文件。它以 .xlsm 扩展名保存。现在excel图标出现了令人恼火的感叹号。 我可以从图标中删除那个感叹号吗? 最佳答案 使用 .xl
我正在尝试从 xlsm 文件复制宏并将其粘贴到新的 xlsm 文件中。我正在研究 win32com 和 xlwings,但没有找到任何有用的东西。非常感谢解决问题的提示! 最佳答案 安装 Python
我有一个 xlsm 文件,其中已经有很少的数据,需要在自动化过程中写入一些数据并创建一个新的 xlsm 文件。使用以下代码创建文件,但它已损坏且无法打开。文件大小减少,例如从 8kb 到 7kb。不确
当用户打开此工作簿时,我想强制他们立即将文件另存为新文件。对话框打开,但它只会让您将其另存为“所有文件”。 Dim Workbook_Orig As Variant Workbook_Orig
我有三个文件,一个是app.xlsm ,另一个是lib.xlsm , 而 app.xlsm使用 lib.xlsm作为引用(在工具 -> 引用中指定)。第三个,third.xlsm有以下代码: Priv
我有两个 Excel 文件,父文件和子文件,其中子文件包含父函数使用的函数库。出于版本控制的目的,我将它们保存在同一个文件夹中,并在完全相同的位置复制和重命名该文件夹以跟踪我的版本。我还希望动态更新引
亲爱的, 我创建了一个代码来循环遍历文件夹并删除 10 年前的文件,但循环无法正常工作,因为 FolderPath(变量)保持不变并且循环遵循无限循环。有帮助吗? Sub LoopThroughFol
需要帮助才能添加命令以另存为 .xlsm:- Private Sub cmdSaveForm1_Click() Dim strFolder As String Dim i As Lon
我的工作场所内部服务器中有一个 xlsm 文件。假设我们有 2 名 worker 使用此文件,其中包括 2 张,My_sheet和His_sheet . 我正在努力寻找一种方法让我们每个人都可以处理“
所以我正在使用 XLTM 文件,我希望用户确保它们保存为 XLSM。当他们单击“保存”时,效果很好,但我发现当他们单击“另存为”时,文件被保存为“*.xlsm.xlsm”。我对如何确保用户保存为 XL
我正在尝试从用密码加密的 xlsm Excel 文件中读取数据。 到目前为止,我在 Apache POI 网站上找到的方法和堆栈溢出都没有成功 这是我到目前为止所尝试的,以及我得到的异常: S
我有一个非常复杂的 Excel 工作簿,格式为 .xlsm,其中包含宏、命名范围以及所有花里胡哨的东西。我需要将 CSV 文件的内容放入此工作簿中的工作表(已存在)中,从最简单的意义上说:第 1 行第
我正在尝试使用 PHPExcel 读取、克隆和写入 .xlsm 文件,但出现错误: fatal error :未捕获异常“异常”,消息为“工作表!G177 -> 公式错误:发生意外错误”... 任何人
是否可以用 phpexcel 读写带有宏文件的 xlsm? 我测试这段代码只是为了复制原始文件: $objReader = PHPExcel_IOFactory::createReader('Exce
我正在编写的宏有问题。我有我正在编写宏的 xlsm 文件,它正在创建一个模板,该模板从其他人发送给我的多个电子表格中获取数据。但是,我用于信息的一个文件是 xlsm 文件,这给我带来了问题。我相信我已
我想启用我的宏.xlsm文件,只能使用 Excel 打开。 我可以知道并拒绝其他试图打开它的应用程序吗? Private Sub Workbook_Open() ... End Sub 最佳答案 您可
我有一个嵌入的 Word 文档 (*.docm)在我的 Excel 工作表中。 Word 文档包含 table , 与其对应的Table 有关系在 Excel 的 WorkSheet . 我希望在 D
我正在努力将我的 XLSM 转换为 XLSX 文件..它是在转换为 PDF 之前,我试图改变一点但没有成功。 我想与工作簿具有相同的名称,但只是 XLSX 格式。 Sub xlsmtoxlsx() D
我现在使用此代码遇到的主要问题是处理我打开的 xlsm 文件的错误。我对这些文件的 VB 代码没有编辑权限。如果 vb 出错,有没有办法跳过文件? 我有一个包含大约 99 个 xlsm 文件的文件夹,
最奇怪的事情发生在我身上。我有一个 xlsm 工作簿,其中包含各个级别的许多宏(常规、工作表、工作簿)。一切都按预期工作,但如果我尝试从 xlsx 工作簿复制一些单元格并将它们粘贴到我的 xlsm 工
我是一名优秀的程序员,十分优秀!