我想知道是否有人可以帮助我。
几个星期以来,我一直在尝试找到一种解决方案,让用户可以执行以下操作:
- 删除有和没有数据的行,
- 移动所有包含数据的行,使它们一个在另一个之下,
- 在保持定义的“输入范围”的同时
我整理了以下脚本,它清除了单元格内容,因此不会改变“输入范围”。
Sub DelRow()
Dim msg
Sheets("Input").Protect "handsoff", userinterfaceonly:=True
Application.EnableCancelKey = xlDisabled
Application.EnableEvents = False
msg = MsgBox("Are you sure you want to delete this row?", vbYesNo)
If msg = vbNo Then Exit Sub
With Selection
Application.Intersect(.Parent.Range("A:S"), .EntireRow).Interior.ColorIndex = xlNone
Application.Intersect(.Parent.Range("T:AE"), .EntireRow).Interior.ColorIndex = 42
Selection.SpecialCells(xlCellTypeConstants).ClearContents
Application.Intersect(.Parent.Range("C:AE"), .EntireRow).Locked = True
Application.Intersect(.Parent.Range("AG:AG"), .EntireRow).Locked = True
End With
Application.EnableEvents = True
End Sub
更新代码
Sub DelRow()
Dim RangeToClear As Range
Dim msg As VbMsgBoxResult
'Sheets("Input").Protect "handsoff", userinterfaceonly:=True
Application.EnableCancelKey = xlDisabled
Application.EnableEvents = False
msg = MsgBox("Are you sure you want to delete this row?", vbYesNo)
If msg = vbNo Then Exit Sub
With Selection
Application.Intersect(.Parent.Range("A:S"), .EntireRow).Interior.ColorIndex = xlNone
Application.Intersect(.Parent.Range("T:AE"), .EntireRow).Interior.ColorIndex = 42
On Error Resume Next
Set RangeToClear = Selection.SpecialCells(xlCellTypeConstants)
On Error GoTo 0 ' or previously defined error handler
If Not RangeToClear Is Nothing Then
RangeToClear.ClearContents
Else
Selection.Sort Key1:=Range("B7"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End If
Application.Intersect(.Parent.Range("C:AE"), .EntireRow).Locked = True
Application.Intersect(.Parent.Range("AG:AG"), .EntireRow).Locked = True
End With
Application.EnableEvents = True
End Sub
但是,问题在于,如果用户选择空白行,他们会收到“错误 400”消息,并且不会将行向上移动以位于彼此下方。
正如我所说,我花了很多时间试图找到一个没有任何成功的解决方案。
如果有人可以看看这个并就我如何实现这一目标提供一些指导,我真的会非常感激。
非常感谢和亲切的问候