我会说我不是VBA的专家。我有一个VBA代码,它在一个单元格中附加选定列表中的多个值。代码有效,但现在我想将代码应用于多个单元格。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To Select Multiple Items from a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$A$7" 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 & "," & Chr(10) & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
如何扩展我的代码以使用单元格范围
$A$7:$A$18
而不是"$A$7"
,以便我可以将VBA代码应用于Excel中的多个单元格。
答案 0 :(得分:5)
更改
If Target.Address = "$A$7" Then
到
If Not Intersect(Target, Range("A7:A18")) Is Nothing Then
答案 1 :(得分:0)
我使用了外卡。
O(n^2)
要
maximum = 0
for idx in range(1, N - 1):
left = min(l[0: idx])
right = min(l[idx + 1:])
middle = l[idx]
if left < middle and right < middle:
new_max = middle - left + middle - right
maximum = max(new_max, maximum)