Is there any alternative to SendKeys
to expand drop-down validation list automatically? I would like to expand drop-down validation list after clicking on a cell. The focus of my question is entirely on how to avoid SendKeys
method.
Here is a properly working solution using SendKeys
:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If HasValidation(Target) Then
SendKeys "%{DOWN}"
SendKeys "{NUMLOCK}", True 'Workaround for Numlock turn off bug
End If
End Sub
Function HasValidation(MyCell As Range) As Boolean
Dim t: t = Null
On Error Resume Next
t = MyCell.Validation.Type
On Error GoTo 0
HasValidation = Not IsNull(t)
End Function
Related links:
HasValidation
function: https://stackoverflow.com/a/31346246/1903793
NumLock
bug: https://stackoverflow.com/a/29551913/1903793
The code above works smoothly without a hitch. I am just biased against SendKeys
for widely reported issues. I suspect that incorporating this solution into larger code might cause unexpected behavior in the future which might be hard to capture.
Your definition of what you wish to achieve leaves a few questions to be answered but perhaps the code below will put you on the right path.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const Trigger As String = "A3:B4" ' modify to suit
Const ValCell As String = "H3" ' modify as required
Dim ValType As Long
If Not Application.Intersect(Target, Range(Trigger)) Is Nothing Then
Application.EnableEvents = False
Range(ValCell).Activate
On Error Resume Next
ValType = ActiveCell.Validation.Type
On Error GoTo 0
If ValType = 3 Then SendKeys "%{DOWN}"
Application.EnableEvents = True
End If
End Sub
The above procedure monitors the Trigger range A3:B4, which you can adjust to what you need. If one of the cells in this range (it could be a single cell range) is clicked the dropdown in the cell having the validation, defined as ValCell , expands, provided it exists. The code achieves this by activating the CellVal
cell.
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.