Excel中的dynamic注释

我有一个名为“OPL”的工作表。 我在column B中有一个下拉列表中的types。 通过查找公式,我已经计算了小时数并将其存储在column P 。 现在我需要将Column P的值作为注释添加到B 。 我有一个代码,这是我想要的。 但问题是我需要运行macros每次我添加一个新的types到列表中。 我希望这发生dynamic,即只要我添加一个新的types,它应该自动采取ist column P相应的值,并作出评论。 我知道我需要通过设置Target在工作表代码中添加此代码,但不知何故我无法实现它。

 Public Sub addComment() Dim row As Integer Dim oldComment As String 'Set start row row = 6 With Sheets("OPL") 'Do until "B" cell is blank Do While .Range("B" & row) <> "" 'If "P" cell is not blank If .Range("P" & row) <> "" Then 'Insert comment for "A" with old if exist .Range("B" & row).addComment ("Dauer : " & .Range("P" & row).Value) End If 'Increase row row = row + 1 Loop End With End Sub 

要自动化这个到B或P列中的值发生变化时附加或更改注释的级别,您将需要一个Worksheet_Change事件macros。

 Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("B:B,P:P"), Range("6:1048576")) Is Nothing Then On Error GoTo bm_Safe_Exit Application.EnableEvents = False Dim bp As Range For Each bp In Intersect(Target, Range("B:B,P:P"), Range("6:1048576")) Range("B" & bp.Row).ClearComments If Not IsEmpty(Cells(bp.Row, "B")) And CBool(Len(Cells(bp.Row, "P").Value)) Then Range("B" & bp.Row).AddComment _ Text:="Dauer : " & Range("P" & bp.Row).Value End If Next bp End If bm_Safe_Exit: Application.EnableEvents = True End Sub 

这应该在列B和/或P中粘贴或清除多个值。