excel中的多选下拉列表
我有很多(超过50)张的工作簿。在49张表格中,E列中或多或少有下拉列表。如果有下拉列表,列表的来源取决于同一行中的C单元格。因此,根据例如C11,E11将是dropdownlist1、dropdownlist2或空白。现在,在49张表单中的每一张中,我想让globaly DropDownList 2成为多个选择列表。下面是我的代码:excel中的多选下拉列表,excel,Excel,我有很多(超过50)张的工作簿。在49张表格中,E列中或多或少有下拉列表。如果有下拉列表,列表的来源取决于同一行中的C单元格。因此,根据例如C11,E11将是dropdownlist1、dropdownlist2或空白。现在,在49张表单中的每一张中,我想让globaly DropDownList 2成为多个选择列表。下面是我的代码: Option Explicit Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Targe
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Sh.Name = "Dane" Then
With Sh
Dim Oldvalue As String
Dim Newvalue As String
.Protect UserInterfaceOnly:=True
Application.EnableEvents = True
On Error GoTo Exitsub
' the check to catch a change of single cell only
If Not Target.Rows.Count > 1 And Target.Columns.Count > 1 Then
' check that this cell in column "E" (concept #2)
If Not Intersect(Target, .Columns(5)) Is Nothing Then
'check if this is validation data cell
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If .Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
End If
Application.EnableEvents = True
End With
End If
Exitsub:
Application.EnableEvents = True
End Sub
现在,如果这个代码在这个工作簿中,它似乎不起作用,并且如果我将下面的内容放入特定的工作表vba中,
Worksheet\u Change
它起作用。此外,目前,该代码将同时适用于dropdownlist1和dropdownlist2。我怎样才能解决这个问题呢?所以我的代码是错误的。一直以来,我都在引用单个单元格,通过以下方式进行检查:如果不是Target.Rows.Count>1和Target.Columns.Count>1,则
但之后和运算符Not都缺失。这样,为了工作,我应该在单独的列中编辑多个单元格。下面的代码是固定的
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Sh.Name = "Dane" Then
With Sh
Dim Oldvalue As String
Dim Newvalue As String
.Protect UserInterfaceOnly:=True
Application.EnableEvents = True
On Error GoTo Exitsub
' the check to catch a change of single cell only
If Not Target.Rows.Count > 1 And Not Target.Columns.Count > 1 Then
' check that this cell in column "E" (concept #2)
If Not Intersect(Target, .Columns(5)) Is Nothing Then
'check if this is validation data cell
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If .Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
End If
Application.EnableEvents = True
End With
End If
Exitsub:
Application.EnableEvents = True
End Sub
尝试在错误行上注释,看看发生了什么。“似乎不起作用”是什么意思?
.Target.Value
不正确,第一个点不应该在那里。您可以使用formula1
属性,即target.validation.formula1
。假设您使用的是列表,它应该显示命名的范围。因此,如果B14的内容包含范围名称,为什么不直接引用它呢?如果target.Rows.Count=1和target.Columns.Count=1,为什么不直接使用然后
,或者更好的是Target.Count=1