[英]Runtime error 6 overflow when looping through worksheets
I am using the code below to remove paths in Excel referencing add-ins for files shared by users. 我使用下面的代码删除Excel中引用用户共享文件的加载项的路径。 It works with some files, but using it with a new file and receiving a Runtime error 6 overflow error upon opening.
它适用于某些文件,但在新文件中使用它并在打开时收到运行时错误6溢出错误。 It is stopping on the Cell Replace line.
它停在Cell Replace行上。
Private Sub Workbook_Open()
Dim i As Long
Dim strWorksheetName As String
strWorksheetName = ActiveSheet.Name
Application.DisplayAlerts = False
For i = 1 To Sheets.Count
Sheets(i).Activate
If Sheets(i).Type = xlWorksheet Then
Cells.Replace What:="'c:\*xla*'!", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End If
Next i
Application.DisplayAlerts = True
Sheets(strWorksheetName).Activate
End Sub
This may be being caused because the Workbook_Open event is firing too soon, before everything is loaded. 这可能是由于Workbook_Open事件在加载所有内容之前过快触发而引起的。
You might have better luck if you don't try to Activate the worksheets. 如果您不尝试激活工作表, 可能会有更好的运气。 (Activating them would cause Excel to have to display them, which it might not be able to do because it is still doing other stuff. But making the changes without forcing the sheets to be displayed may let it continue doing what it wants to do. I'm guessing, but until someone comes up with another solution, a guess is as good as anything!)
(激活它们会导致Excel必须显示它们,它可能无法执行,因为它仍在执行其他操作。但是在不强制显示工作表的情况下进行更改可能会让它继续执行它想要执行的操作。我猜,但是直到有人想出另一个解决方案,猜测就像任何东西一样好!)
Try changing your code to: 尝试将代码更改为:
Private Sub Workbook_Open()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws in Worksheets
ws.Cells.Replace What:="'c:\*xla*'!", _
Replacement:="", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next
Application.DisplayAlerts = True
End Sub
If it works, I'll leave the answer here. 如果它有效,我会在这里留下答案。 If it doesn't, I'll delete the answer and someone else can make a suggestion.
如果没有,我会删除答案,其他人可以提出建议。
It is possible that one or more 'sheets' is/are not .Visible . 有可能一个或多个“纸张”不是。 可见 。 You cannot .Activate a 'sheet' if it the .Visible property is False or xlVeryHidden.
你不能。如果.Visible属性为False或xlVeryHidden,则激活 'sheet'。
Option Explicit
Private Sub Workbook_Open()
Dim i As Long
Application.DisplayAlerts = False
For i = 1 To Worksheets.Count
With Worksheets(i)
.Cells.Replace What:="'c:\*xla*'!", _
Replacement:=vbNullString, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End With
Next i
End Sub
¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals. ¹ 请参阅如何避免在Excel VBA宏中使用选择以获取更多方法, 以避免依赖选择和激活来实现目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.