简体   繁体   中英

Calculate Event: Insert Comment In Cell If Value and Delete Any Comments If Not Value

The following code is a change event handler that searches col B for the word "fee" and inserts comments in the 3 adjacent cols if the word "fee" is found in col B:

Private Sub Worksheet_Calculate()

Dim rng As Range, cell As Range

Set rng = Range("B:B")

If Not rng Is Nothing Then

    For Each cell In rng.Cells
        If cell.Value = "fee" Then
            cell.Offset(0, 1).AddComment "fi"
            cell.Offset(0, 2).AddComment "fo"
            cell.Offset(0, 3).AddComment "fum"
        End If
    Next
End If

End Sub

The above code works fine.

I also want to search col B and delete any existing comments in the 3 adjacent cols if the word "fee" does not occur in col B. So, I added an Else statement:

Private Sub Worksheet_Calculate()

Dim rng As Range, cell As Range

Set rng = Range("B:B")

If Not rng Is Nothing Then

    For Each cell In rng.Cells
        If cell.Value = "fee" Then
            cell.Offset(0, 1).AddComment "fi"
            cell.Offset(0, 2).AddComment "fo"
            cell.Offset(0, 3).AddComment "fum"
        Else:
            cell.Offset(0, 1).Comment.Delete
            cell.Offset(0, 2).Comment.Delete
            cell.Offset(0, 3).Comment.Delete

        End If
    Next
End If

End Sub

This results in the runtime error: "Object variable or With block variable not set", and the debugger points to

cell.Offset(0, 1).Comment.Delete

VBA seems to want me to use a With statement, but the With permutations I've tried result in the same error. Any thoughts?

Follow up with Andy's correct suggestion. The code adds comments if the condition is met, clears comments if it is not:

Private Sub Worksheet_Calculate()

Dim rng As Range, cell As Range
Set rng = Range("B:B")

If Not rng Is Nothing Then
    For Each cell In rng.Cells
        cell.Offset(0, 1).ClearComments
        cell.Offset(0, 2).ClearComments
        cell.Offset(0, 3).ClearComments
        If cell.Value = "fee" Then
            cell.Offset(0, 1).AddComment "fi"
            cell.Offset(0, 2).AddComment "fo"
            cell.Offset(0, 3).AddComment "fum"
        End If
    Next
End If

End Sub

VBA is not suggesting that you use With. The error occurs if you attempt to Delete a comment when there isn't one.

You can either check for the existence of a comment before attempting to Delete it:

Dim cmt As Comment

Set cmt = Range("A1").Comment
If Not cmt Is Nothing Then
    Range("A1").Comment.Delete
End If

or, simpler, use ClearComments :

Range("A1").ClearComments

Also note that your first code is on the Calculate event, not Change .

Delete the colon after Else as well - always have Else as a single word on its own line; this colon may cause issues.

Added following OPs coded solution: Your code could be simplified:

If Not rng Is Nothing Then

    For Each cell In rng.Cells
        cell.Offset(0, 1).ClearComments
        cell.Offset(0, 2).ClearComments
        cell.Offset(0, 3).ClearComments
        'or even just
        'cell.Range("B1:D1").ClearComments

        If cell.Value = "fee" Then
            cell.Offset(0, 1).AddComment "fi"
            cell.Offset(0, 2).AddComment "fo"
            cell.Offset(0, 3).AddComment "fum"
        End If
    Next
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM