简体   繁体   中英

Excel vba remove tooltip if cell blank

I have a vba that when I click in a cell (that contains "ID's" it searches in another sheet for that "ID" and creates a tooltip with information from that sheet. It's working good as follows:

  • If ID exists, shows the tooltip when I click it. Hides tooltip if I click in an empty cell;
  • If the ID does not exists, it shows nothing;

My problem remains in the follow step:

  • If I had an ID that exists and remove it from the cell, the tooltip remains there but without information. Each time I click in that cell, it shows the empty tooltip. I need it to not show anything like the cell is empty.

My Code:

Public sTarget As String

Private Sub Worksheet_Change(ByVal Target As Range)

sTarget = Target.Address

Dim MyVal As String
Dim MyToolTipBody As String
Dim MyToolTipHead As String
Dim Rng As String

On Error Resume Next
MyVal = "*" & Range(sTarget).Value
With Worksheets("Sheet2")
On Error Resume Next
    Rng = .Cells.Find(What:=MyVal, LookIn:=xlFormulas, LookAt:=xlWhole).Address
    MyToolTipHead = 'Code for tooltip header
    MyToolTipBody = 'Code for tooltip body
End With

With Range(sTarget).Validation
    .Delete
    .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
    :=xlBetween
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = MyToolTipHead
    .ErrorTitle = ""
    .InputMessage = MyToolTipBody
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = False
End With
End Sub

Any suggestions in how to achieve this? Thanks.

I have two options for you:

  1. Detect a cell becomes empty ("") using a Worksheet_change event and delete validation to remove tooltip
  2. Use your current Select event but introduce an IF block to say "If the cell is empty, then remove validation, otherwise create a tooltip..." to ensure validation (and therefore your tooltip) is deleted when you click on a blank cell.

I think the best is definitely Option 1. I've included Option 2 just so you can see how it can be done with just your current event and how that logically follows on to using Worksheet_change for the tooltip removal instead.

Option 1

Add a worksheet change event that does this:

Private Sub Worksheet_Change(ByVal Target As Range)

   If Target.value = "" Then
      Target.Validation.Delete
   End If

End Sub

This will trigger when you delete your ID from a cell; then your Target.Value will equal "" and thus any validation will be deleted automatically too; taking away the tooltip.

The unintended consquence is that this removes all validation; so if you want to keep certain other validation (like data type, List) you will have to re-add it to the cell; you could write a subroutine for just this (eg sub putValidationBack(x_in as Range).. )

Option 2

Add an IF block around your current code:

if Range(sTarget).Value = "" then
   With Range(sTarget).Validation
      .Delete
   End with
Else
  ' Everything after and including your On Error Resume goes here

End if

Same consquence as above this will remove all validation so you'll need to put back any validation you want to keep.

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