2015-12-30 58 views
0

我分享在工作中多同事的工作簿時,我試圖評論在一定期限內添加到單元格(F47)(期間選擇作爲數字列表)在單元格J1中。添加由VBA細胞評論基於單元格的值,然後將其刪除價值變動

如果期間= 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 

我得到一個運行時1004錯誤,如果我與消息(J1)選擇關閉「應用程序定義或對象定義的錯誤」這凸顯以下

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") 

回答

3

你需要清除代碼任何現有註釋第一:

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 

你可能會更好使用Worksheet_Change事件和監控J1 - 除非包含公式。

+0

完美地工作,謝謝。 –

+0

順便說一句,你可以使用'ActiveSheet.Cells.ClearComments'而不是循環。 – Rory

+0

已經刪除了循環,並且像你建議使用'ActiveSheet.Cells.ClearComments' –

相關問題