B16
=SUMIFS($B$7:$B$14;$C$7:$C$14;C16;$D$7:$D$14;D16;$E$7:$E$14;E16)
在性别/类型/状态 (C16/D16/E16) 下面的第 16 行中,我创建一个下拉菜单并
我已经使用此 VBA 代码为 State(E16) 列表启用了选择多个项目
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("E16")) Is Nothing Then
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
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
1/ 现在,如果我同时选择多个值,例如:VN和BD,B16中的返回值= 0,则sumifs函数不起作用,我知道如果我将B16中的代码更改为:
=SUM(SUMIFS($B$7:$B$14;$C$7:$C$14;C16;$D$7:$D$14;D16;$E$7:$E$14;{"VN";"BD"}))
既然您无论如何都在使用 VBA,这种情况下最简单的解决方案似乎是定义一个简单的 UDF 函数(在标准模块中):
Function ToArray(list As String)
ToArray = Split(list, "; ")
End Function
并在
SUMIFS
公式中使用它:
=SUM(SUMIFS($B$7:$B$14;$C$7:$C$14;C16;$D$7:$D$14;D16;$E$7:$E$14;ToArray(E16)))
另外,当涉及到事件过程时,其逻辑结构可以简化一些。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String, Newvalue As String
On Error GoTo Exitsub
If Not Intersect(Target, Range("E16")) Is Nothing Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
ElseIf Target.Value = "" Then
GoTo Exitsub
Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
ElseIf InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & "; " & Newvalue
End If
End If
End If
Exitsub:
Application.EnableEvents = True
End Sub