在Excel注释中应用公式

时间:2015-06-02 14:11:31

标签: excel vba comments formula cells

我想评论一个动态单元格。例如,如果我更改单元格中的值,则注释应该更改。我试过了:

Set wb = ActiveWorkbook
file = Application.GetOpenFilename
Set wb2 = Workbooks.Open(file)
ws2 = ActiveWorkbook.Worksheets.Count
For J = 1 To ws2
    ThisWorkbook.Worksheets(J).Activate
    For x2 = 18 To 245
        For y2 = 1 To 23
        Worksheets(J).Select
        wb2.Activate
        Application.DisplayAlerts = False
        Cells(x2, y2).Select
        Selection.ClearNotes
        Selection.NoteText Text:=wb.Worksheets(J).Cells(x2, y2).Value
        Next y2
    Next x2
Next J

并且代码可以复制单元格,但不能动态复制。如果我将A更改为B,我需要在评论中添加B

如果我更改同一行,此代码有效:

Private Sub Worksheet_Change(ByVal Target As Range)
      '//clearing more than one cell causes an error
      On Error Resume Next
      '//(can't overwrite an existing comment)
      Target.ClearComments
      ActiveCell.Select
      With Selection
            'get the previous value when value changes
            .AddComment
            .Comment.Visible = False
            .Comment.Text Text:="" & Target
      End With
End Sub

结果,如果你验证(没有输入其他评论是在下一行)你有像单元格的评论。现在我需要在另一个文件中使用相同的情况。

由于

1 个答案:

答案 0 :(得分:0)

这里是受害者的最终代码。

   Option Explicit
Dim newRange As Range
Public wbx As Workbook
Dim x, y As Integer
Private Sub Worksheet_Change(ByVal Target As Range)

On Error Resume Next

x = ActiveCell.row
y = ActiveCell.Column

Workbooks("Test2.xlsb").Activate

Target.ClearComments

'Sheets(2).Activate
ActiveSheet.Range("A1").Select
ActiveCell.Offset(x - 2, y - 1).Select

    With Selection
        .AddComment
        .Comment.Visible = False
        .Comment.Text Text:="" & Target
    End With

Workbooks("Test1.xlsb").Activate


End Sub