VBA适用于特定范围

时间:2016-10-18 17:29:05

标签: excel-vba vba excel

我有以下代码,允许我在数据验证下选择多个项目。但是,我有两组数据验证,我只希望此代码适用于Range(“K41:K60,K73:K107)请注意。谢谢!

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler


On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Intersect(Target, rngDV) Is Nothing Then


Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
 If oldVal = "" Then

 Else
 If newVal = "" Then

 Else
 Target.Value = oldVal _
   & ", " & newVal


 End If
 End If
 End If


 exitHandler:
 Application.EnableEvents = True
 End Sub

1 个答案:

答案 0 :(得分:0)

你的意思是你只想在该范围内的单元格被更改时触发此代码吗?如果是这样,也许修改这一行如下

If Intersect(Target, Range("K41:K60, K73:K107")) Is Nothing Then