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:
My problem remains in the follow step:
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:
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.