vba - VBA 代码不限于指定列
问题描述
我试图将以下代码限制为仅第 6 列和第 7 列,但它适用于整个工作表。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lOld As Long
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
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 6 _
Or Target.Column = 7 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If
End If
If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
解决方案
您已经在测试内部INTERSECT
和之后在该测试之外复制了代码。外面的列有一些测试,所以我不确定它为什么会触发......似乎还有一个额外的End If
我无法弄清楚,所以我不确定它是如何执行的。
我重写了删除多余的嵌套 Ifs 和诸如此类的东西。我添加评论主要是为了在我重写时帮助我,但它们可能对未来的编辑有用。
此代码仅针对xlCellTypeAllValidation
第 6 列和第 7 列中的类型的单元格运行。如果您不需要仅限于单元xlCellTypeAllValidation
格,则将其从主If
测试中删除。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lOld As Long
'Exit routine if more than one cell was changed
If Target.Count > 1 Then GoTo exitHandler
'Shut off errors, and attempt to grab xlCellTypeAllValidation cells
'If no cells are of type xlCellTypeAllValidation then exit routine
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
If rngDV Is Nothing Then GoTo exitHandler
'If the cell changed is xlCellTypeAllValidation AND in columns 6 or 7 Then run code
If Not Intersect(Target, rngDV) Is Nothing AND (Target.Column = 6 OR Target.Column = 7) Then
'Shut off events
Application.EnableEvents = False
'Capture old and new values to variables
newVal = Target.Value
Application.Undo
oldVal = Target.Value
'undo the undo
Target.Value = newVal
'If the cell used to hold a value and it was changed to a new value (not null)
If oldVal <> "" AND newVal <> "" Then
'Test to see if the change didn't affect the contents of the cell
lOld = Len(oldVal)
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else 'They've truly changed the content, so bring in the old content and append the new with a comma
Target.Value = oldVal & ", " & newVal
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
推荐阅读
- sql-server - Azure SQL 数据库未找到或无法访问的问题
- git - 如何 git 合并,但不使用最新的提交
- php - 同一页面上的 AJAX 表单的动态数量
- angular - 在Angular 6中测试httpClient的继承
- cordova - 更新 Cordova 项目 Android 平台
- dji-sdk - DJI 板载 sdk 用于避障
- javascript - 谷歌折线图滚动/缩放问题
- spring - 在 Spring MVC 中使用 @PathVariable 注释时出现 HTTP 404 错误
- python-2.7 - Python - 如何将保存表模式的 .txt/.csv 文件转换为 .avsc 文件
- amazon-web-services - 打开没有这样的文件或目录