excel - 代码在不正确的不同列上运行
问题描述
我有一个在目标列上完美运行的代码。但问题是当有人插入一个新列时,代码无法识别该列由于插入的列而发生了移动。
当代码运行时,它会在错误的列上运行。
有人可以帮助我如何确保代码在所需的列上运行,即使插入了新列。
请看下面的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
On Error GoTo Exitsub
If Target.Column = 24 And Target.Row > 4 And Target.Row < 500 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
Target.Value = Oldvalue & ", " & Newvalue
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
如果插入了新列,这就是运行完美但在错误列上的代码。
解决方案
推荐阅读
- javascript - Zapier Javascript: Unexpected token ] - 在 IDE、Repl.it 和 Node v6.3.1 中工作
- javascript - 从对象内的所有对象中按键移除元素
- php - 使用来自一个视图的输入将结果打印到另一个视图
- c# - C#比较多种类型
- raspberry-pi3 - scp 冻结并创建空文件
- php - Laravel 在雄辩的保存上试运行
- amazon-web-services - 使用 aws java sdk 触发 lambda 函数的 SQS 的返回值
- python - Cython 给出“不必要的”警告
- apache-spark - 前向填充多列可重用功能代码
- c++ - 在 C++ 中使用 AssocQueryString 获取可执行路径