我想知道是否有人能够帮助我请。删除行和维护输入范围
这几周我一直在试图找到一个解决方案,使用户可以执行以下操作:使用和不使用数据
- 删除行,
- 移的所有行包含数据的麻生太郎,他们坐一个在另一个
- ,同时保持一个定义的“输入范围”
我已经把下面的脚本,它会清除单元格的内容,因此不会改变“输入范围” 。
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”消息,并且它不行上移坐在对方下面。
正如我所说,我花了这么多时间试图找到一个没有任何成功的解决方案。
我真的很感激,如果有人可以看看这个请给我提供一些指导我可以做到这一点。
许多的感谢和亲切的问候
根据http://support.microsoft.com/?kbid=146864至少在Excel 97(!)中,运行时错误400是“窗体已显示;无法模态显示”。这似乎并不适用。 “ClearContents”行上的错误?错误的措辞是什么? – 2013-02-09 15:20:11
嗨@DougGlancy,感谢您花时间回复我的帖子。错误只是说'400'。如果有帮助,我已经在这里设置了一个测试文件:https://www.box.com/s/cnptwwmnmzoooirrgos2。您将在第8-11行中看到B列中有数据。然后我将数据添加到第46行和第47行,再次添加到第B列中。如果突出显示其中的空白行并尝试删除它们,您将收到有问题的错误。非常感谢和亲切的问候 – IRHM 2013-02-09 15:32:18
嗯,我看到一个错误,如果选择是空白的,那么用你的'ClearContents'行,所以我会回答这个问题,看看它是否有帮助。 – 2013-02-09 15:34:12