Ște*_*aga 6 excel vba comments excel-vba
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
Run Code Online (Sandbox Code Playgroud)
另一组显示问题的代码.(在新工作簿中使用三个空白工作表运行此命令.):
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
Run Code Online (Sandbox Code Playgroud)
我找到了解决方法,但仍然不知道为什么会出现这个问题。由于某种原因,当您选择了多个工作表时,会发生错误。解决方案是... 在添加注释之前选择一张纸someSheet.Select
。在宏结束时,如果需要,您可以尝试再次选择所有先前选择的工作表。