excel - 将超链接添加到单元格时出现运行时错误
问题描述
执行数月无错误后莫名出现
执行以下代码时,我一直遇到运行时错误 1004。几个月来我一直在运行完全相同的代码而没有问题。今天,我添加了一个新的工作表并编辑了用户窗体上一些控件的位置,并添加了一些新的。这两种行为都影响了与问题完全无关的事情,但我提到它是因为我不知道它可能是什么。
该错误发生在该行上,.Hyperlinks.Add
并且似乎不是由正在使用的变量的意外值引起的。有关这些变量的值的注释,请参见下面的代码片段。我在这里不知所措,这段代码从来没有出现过问题,但现在它神奇地出现了,并且变量具有预期值。我认为它必须与工作表设置或工作簿设置有关。我正在处理受保护的工作表,尽管这不是什么新鲜事,也没有引起过问题。工作表受到保护,使用ws.Protect Password:="abcd", UserInterfaceOnly:=True
它应该允许 VBA 仍然可以毫无问题地编辑受保护的工作表。
关于可能导致此问题的任何想法?
Public Sub Write_Next_LineItem(ByVal rowNum As Integer, ByRef arr1() As Variant, ByVal strHash As String, Optional ByVal IsPartsOrder As Boolean = False)
Dim startRow As Integer, j As Integer, entryCount As Integer, strFormula As String
If fastMode = False Then Disable_Slowdowns 'turns off screen updating, Excel events, and switches calculation mode to xlCalculationManual
startRow = ThisWorkbook.Sheets("Overview").Range("rng_ProjectList").Cells(1, 1).Row
entryCount = 0
With ThisWorkbook.Sheets("Overview")
'Note that "Overview" is in fact a valid sheet name, evident by the value of startRow which was
'set by referencing the sheet a few lines above
'<<<<< code here that determines the value of entryCount. At this point, startRow = 14, entryCount = 0
'***********************THE NEXT LINE IS CAUSING THE ERROR*************************
.Hyperlinks.Add Anchor:=.Cells(startRow + entryCount, 2), Address:="", SubAddress:=wsName & "R1C1"
'the error occurs with the following values in these variables:
'startrow = 14
'entrycount = any number (tested with entryCount = 0, 1, 2)
'wsName = 'Column-Order'!
'where Column-Order is the name of a worksheet that was just
'added programmatically, and is a valid worksheet name (I've triple checked)
'other code here, but is not executed due to the above error
End With
End Sub
[编辑]:我在下面包含了 fastMode 的代码。这不是导致问题的快速模式。至于要求我正在编辑的代码的评论,就像我说它是一个用户表单和一个新工作表一样,但是在这个错误期间根本没有使用它,所以我不会在这里展示或者这篇文章会变成大约 4 倍长。
'In a separate code module:
Public fastMode As Boolean
Public Sub Disable_Slowdowns(Optional ByVal AllowScreenUpdating As Boolean = False, Optional ByVal AllowCalculations As Boolean = False, Optional ByVal AllowEvents As Boolean = False)
On Error Resume Next
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
fastMode = True
End Sub
[编辑#2]:好吧,完全没有再次更改代码,我今天打开它,由于一些令人难以置信的加重原因,它不再有这个问题。我现在完全困惑了。我已经在这个 VBA 项目上工作了 2 年的大部分时间,我从来没有经历过像这样不稳定和无法解释的事情。我想我应该很高兴它的工作,但这会严重困扰我。
解决方案
推荐阅读
- php - 通过提及键从数组中获取特定的键/值对
- python - 在 Windows Server 2019 上,通过 cgi/Apache 调用的 pyttsx3 生成空的 wav 文件
- python - Python:六边形计数图
- android - 根据 byteBuffer 改变位图像素
- cmake - 是否可以在 CMakeLists.txt 中引用额外的 .txt 文件?
- html - 有没有办法强制 div 内的浮动 div 元素仅使用 css 和 html 垂直占据可用空间?
- java - Maven将layers.idx和其他spring类自动添加到war文件中
- android - 如何在本机 C++ 中使用 AudioRecord 类录制音频
- c++ - std::condition_variable 内存写入可见性
- excel-formula - 如何根据 Google 表格中的付款条件更改到期日期