Limit Target Range with Selection Change in VBA

Copper Contributor

My objective is to show the contents of a formulas cell that is looking up a list of descriptions to match item codes (around 500) in a Pivot table (the existing table descriptions were free text and create too many rows for the each item code). The look up item list is using a new set of standard descriptions from another query table. The descriptions can be very long and I want to keep the description column narrow by only showing the description if the cell if column A is selected.

I have VBA code to show the contents of the formula cell when selected. This works okay for the column A target. However, the comments are created and don't delete when I move out of column A and another cell is selected. Can this code be modified to delete the comment when any other cell (restrict to Column A) is selected in the worksheet.

 

Private LastTarget As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

 

If Target.Column = 1 Then

On Error Resume Next

  1. If Not LastTarget Is Nothing Then
  2.      If Not LastTarget.Comment Is Nothing Then LastTarget.Comment.Delete
  3.          
  4.  End If
  5.  
  6.        
  7.  If Not Trim$(Target.Value) = vbNullString Then
  8.      If Target.Comment Is Nothing Then
  9.          Target.AddComment Target.Text
  10.          Target.Comment.Visible = True
  11.          Target.Comment.Shape.Width = 300 'Change as needed
  12.          Target.Comment.Shape.Height = 50 'Change as needed
  13.          Target.Comment.Shape.Fill.Transparency = 0#  'Make the comment a little see through
  14.      End If
  15.      
  16.  End If
  17.     
  18.  Set LastTarget = Target

End If

0 Replies