【发布时间】:2017-07-12 13:19:48
【问题描述】:
AddComment 语法适用于工作簿中的第一个选定工作表,但下一个工作表给我这个错误:错误 1004“应用程序定义或对象定义错误”。我不知道如果选择了多张工作表并且仅适用于第一个选择的工作表,为什么会崩溃。有人有什么想法吗?
If selectedSheet.Cells(7, columnIndex).value <> 100 Then
selectedSheet.Cells(7, columnIndex).Interior.ColorIndex = 3
If standardReportFilePath <> "" Then 'not using the Standard Report Evalution algorithm
If VerifyStandardReportFile(selectedSheet.Name, selectedSheet.Cells(1, columnIndex).value, wbk, amplitude, missingCrashes) = True Then
selectedSheet.Cells(1, columnIndex).Interior.ColorIndex = 36 ' color the crash cell with yellow
Set rng = selectedSheet.Cells(1, columnIndex)
If rng.Comment Is Nothing Then
**rng.AddComment "In Standard Report this crash starts to deploy from " & CStr(amplitude) & " amplitude"**
Else
rng.Comment.Text "In Standard Report this crash starts to deploy from " & CStr(amplitude) & " amplitude"
End If
End If
End If
End If
End If
显示问题的备用代码集。 (在新工作簿中使用三个空白工作表运行此操作。):
Sub test()
Dim ws As Worksheet
Dim Rng As Range
'Running code with a single sheet selected
Worksheets("Sheet1").Select
'Code that shows issue - this will work
Set ws = Worksheets("Sheet2")
Set Rng = ws.Cells(1, 1)
If Rng.Comment Is Nothing Then
Rng.AddComment "xxx"
End If
'Get rid of comment again
Rng.Comment.Delete
'Running code with multiple sheets selected
Worksheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
'Code that shows issue - will crash on the "AddComment"
Set ws = Worksheets("Sheet2")
Set Rng = ws.Cells(1, 1)
If Rng.Comment Is Nothing Then
Rng.AddComment "xxx"
End If
End Sub
【问题讨论】:
-
尝试
ActivateselectedSheet每次设置一个新的。 -
我做到了。没有成功:(
-
另一个想法,也许是
amplitude的问题?很抱歉没有检查这个,但我无法访问我的计算机 atm。 -
不,幅度很好。值存在。在弹出错误时的备忘录中,该值为-15。
-
@ȘtefanBlaga 我编辑了问题以包含我认为是 MCVE 的内容。希望这将使人们的注意力集中在实际问题上,而不是他们可能认为会影响事情的任何其他事情上。如果您不喜欢添加 MCVE,请随时回滚到问题的先前版本。