Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA根据单元格值向单元格添加注释,然后在值更改时将其删除_Vba_Excel - Fatal编程技术网

VBA根据单元格值向单元格添加注释,然后在值更改时将其删除

VBA根据单元格值向单元格添加注释,然后在值更改时将其删除,vba,excel,Vba,Excel,我与多名同事共享一份工作簿,并试图在单元格J1的某个时段(时段被选为数字列表)向单元格(F47)添加注释 如果句点=8,我想添加注释。 如果句点不等于8,我想删除/隐藏注释 Sub Comment_Delete() Dim C As Comment Worksheets("Statement").Activate For Each C In ActiveSheet.Comments C.Delete Next End Sub Private Sub Worksheet_Se

我与多名同事共享一份工作簿,并试图在单元格J1的某个时段(时段被选为数字列表)向单元格(F47)添加注释

如果句点=8,我想添加注释。 如果句点不等于8,我想删除/隐藏注释

Sub Comment_Delete()

 Dim C As Comment

 Worksheets("Statement").Activate

 For Each C In ActiveSheet.Comments
  C.Delete
 Next

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

 Set Period = Range("J1")
 Set Target = Range("F47")

 If Period.Value = 8 Then
     Target.AddComment ("If balance is meant to be negative but = 0, the debtor was invoiced in P8 and balance was paid off, see data sheet P* Bal Paid")

 Else: Call Comment_Delete
 End If

 End Sub
如果我选择关闭(J1)并显示消息“Application Defined or Object Defined Error”(应用程序定义或对象定义错误),则会出现运行时1004错误,该消息突出显示了下面的代码

Target.AddComment ("If balance is meant to be negative but = 0, the debtor was invoiced in P8 and balance was paid off, see data sheet P* Bal Paid")

您需要首先清除任何现有注释:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Set Period = Range("J1")
    Set Target = Range("F47")

    If Period.Value = 8 Then
        If Not Target.Comment Is Nothing Then Target.Comment.Delete
        Target.AddComment "If balance is meant to be negative but = 0"

    Else: Call Comment_Delete
    End If

End Sub

您最好使用
工作表\u Change
事件和监控J1-除非其中包含公式。

您需要首先清除任何现有注释:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Set Period = Range("J1")
    Set Target = Range("F47")

    If Period.Value = 8 Then
        If Not Target.Comment Is Nothing Then Target.Comment.Delete
        Target.AddComment "If balance is meant to be negative but = 0"

    Else: Call Comment_Delete
    End If

End Sub

您可能最好使用
工作表\u Change
事件和监控J1-除非其中包含公式。

非常有效,谢谢。顺便说一句,您可以使用
ActiveSheet.Cells.ClearComments
而不是循环。已经删除了循环,并且像您建议的那样使用“ActiveSheet.Cells.ClearComments”代替循环非常有效,谢谢。顺便说一句,你可以使用
ActiveSheet.Cells.ClearComments
而不是循环。你已经删除了循环,并且像你建议的那样使用了“ActiveSheet.Cells.ClearComments”