在Excel VBA中仅选择特定单元格范围的下拉列表?
我正在尝试弹出一个选项,用户可以从列表中进行选择,并将其输入到单元格中,值之间用逗号分隔。我让我的VBA代码发挥作用,但我只想将其绑定到特定的单元格范围,而不是基本上任何单元格范围中的数据验证列表在Excel VBA中仅选择特定单元格范围的下拉列表?,excel,vba,list,Excel,Vba,List,我正在尝试弹出一个选项,用户可以从列表中进行选择,并将其输入到单元格中,值之间用逗号分隔。我让我的VBA代码发挥作用,但我只想将其绑定到特定的单元格范围,而不是基本上任何单元格范围中的数据验证列表 Option Explicit Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim rngDV As Range Dim oldVal As String Dim newVal As String Dim strList
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strList As String
On Error Resume Next
Application.EnableEvents = False
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Not Intersect(Target, rngDV) Is Nothing Then
If Target.Validation.Type = 3 Then
strList = Target.Validation.Formula1
strList = Right(strList, Len(strList) - 1)
strDVList = strList
frmDVList.Show
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strSep As String
strSep = ", "
Application.EnableEvents = False
On Error Resume Next
If Target.Count > 1 Then GoTo exitHandler
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If newVal = "" Then
'do nothing
Else
If oldVal = "" Then
Target.Value = newVal
Else
Target.Value = oldVal & strSep & newVal
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
我想我需要更新私有子工作表\u SelectionChange(ByVal Target As Range)
但是当我选择一个单元格区域,如G1:G100,它会导致调试问题
另外,如何让数据验证列表始终显示在“G”列中
frmDVList
=未定义或设置strDVList
=未定义。你的意思是什么?不知道它是否能解决你的问题,但你应该有一句话:Dim strDVList as String,frmDVList as Object
或其他什么。还有一行类似于Set frmDVList=…
。尤其是当顶部有选项Explicit
时,如果希望在该列中选择单元格时显示下拉列表,则可以使用sendkeys,但通常也会切换numlockSendKeys“%{UP}”
不幸的是,这并不能真正解决我的问题。。