【发布时间】:2017-11-29 13:09:26
【问题描述】:
我在当前使用 BeforeDoubleClick 的工作表上有代码。此代码在任何包含数据验证公式的双击单元格上放置一个组合框。组合框采用公式。
我喜欢这个,因为您获得了数据验证列表,但您也可以选择让用户在组合框中键入并获得数据验证选项的自动完成。
工作表本身有两个数据验证区域,一个用于州,一个用于城市。城市数据验证取决于州的选择。
我真的不喜欢用户每次想要更改选择时都必须双击。如果他们可以单击并弹出它,那就太好了。
感谢任何帮助!
工作表代码: (来自http://www.contextures.com/xlDataVal11.html#works) ((他们有另一个链接 (http://www.contextures.com/xlDataVal14.html) 显示如何通过单击执行此操作,但是当我尝试在 =INDIRECT 部分添加(允许相关数据验证)时,它不起作用。)
'==========================
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains
'a data validation list
Cancel = True
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
'for simple INDIRECT function (English)
' e.g. =INDIRECT(B2)
'will create dependent list of items
If Left(str, 4) = "INDI" Then
lSplit = InStr(1, str, "(")
str = Right(str, Len(str) - lSplit)
str = Left(str, Len(str) - 1)
str = Range(str).Value
End If
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 4
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.TempCombo.DropDown
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'=========================================
Private Sub TempCombo_LostFocus()
With Me.TempCombo
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End Sub
'====================================
Private Sub TempCombo_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
Select Case KeyCode
Case 9 'Tab
ActiveCell.Offset(0, 1).Activate
Case 13 'Enter
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
【问题讨论】:
标签: excel validation combobox onclick