我想知道是否有人能夠幫助我請。刪除行和維護輸入範圍
這幾周我一直在試圖找到一個解決方案,使用戶可以執行以下操作:使用和不使用數據
- 刪除行,
- 移的所有行包含數據的麻生太郎,他們坐一個在另一個
- ,同時保持一個定義的「輸入範圍」
我已經把下面的腳本,它會清除單元格的內容,因此不會改變「輸入範圍」 。
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