excel - 如果目标地址 - 我如何使用范围?
问题描述
嗨,我目前正在进行多列表验证,但目前我只能将其添加到一个单元格 E2,并且我希望将其设置在从 E2:E40 开始的范围内。我在考虑$E$2:$E$40。但是,这不起作用。
Private Sub Worksheet_Change(ByVal Target As Range)
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$E$2" 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
解决方案
使用这样的东西
Dim AffectedCells As Range
Set AffectedCells = Intersect(Me.Range("E2:E40"), Target)
If Not AffectedCells Is Nothing Then
' do your stuff here …
End If
AffectedCells
包含E2:E40
其中实际更改的所有单元格。
确保循环AffectedCells
处理每个单元格
Dim Cell As Range
For Each Cell In AffectedCells.Cells
' do your stuff with each Cell here …
Next Cell
推荐阅读
- discord - Discord.py 音乐机器人队列
- c# - 应用程序初始化 SQL 连接时发生 System.TypeInitializationException
- mongodb - 如何在 mongodb 中保存模式定义
- python - 检测 google sheet 中的变化并执行 actionsheet API
- kernel-density - 有没有一种可靠的方法来计算一组轨迹的概率密度函数?
- api - 在 api 中进行身份验证时遇到问题
- regex - 带有扩展字符的缓冲区字竞争配置(neovim)
- python - Matplotlib 动画在 Spyder 中不起作用
- javascript - 如何将包含类的选项放在选项的开头
- javascript - 如何在 Html 表中添加新行和列