2013-02-09 67 views
0

我想知道是否有人能夠幫助我請。刪除行和維護輸入範圍

這幾周我一直在試圖找到一個解決方案,使用戶可以執行以下操作:使用和不使用數據

  • 刪除行,
  • 移的所有行包含數據的麻生太郎,他們坐一個在另一個
  • ,同時保持一個定義的「輸入範圍」

我已經把下面的腳本,它會清除單元格的內容,因此不會改變「輸入範圍」 。

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」消息,並且它不行上移坐在對方下面。

正如我所說,我花了這麼多時間試圖找到一個沒有任何成功的解決方案。

我真的很感激,如果有人可以看看這個請給我提供一些指導我可以做到這一點。

許多的感謝和親切的問候

+0

根據http://support.microsoft.com/?kbid=146864至少在Excel 97(!)中,運行時錯誤400是「窗體已顯示;無法模態顯示」。這似乎並不適用。 「ClearContents」行上的錯誤?錯誤的措辭是什麼? – 2013-02-09 15:20:11

+0

嗨@DougGlancy,感謝您花時間回覆我的帖子。錯誤只是說'400'。如果有幫助,我已經在這裏設置了一個測試文件:https://www.box.com/s/cnptwwmnmzoooirrgos2。您將在第8-11行中看到B列中有數據。然後我將數據添加到第46行和第47行,再次添加到第B列中。如果突出顯示其中的空白行並嘗試刪除它們,您將收到有問題的錯誤。非常感謝和親切的問候 – IRHM 2013-02-09 15:32:18

+0

嗯,我看到一個錯誤,如果選擇是空白的,那麼用你的'ClearContents'行,所以我會回答這個問題,看看它是否有幫助。 – 2013-02-09 15:34:12

回答

0

如果選擇爲空,行Selection.SpecialCells(xlCellTypeConstants).ClearContents 將失敗,因爲沒有xlCellTypeConstants。你需要測試這一點,只要有任何明確的內容:

編輯:爲了回答排序問題

我覺得你只是想不管什麼樣的,所以我就搬到了SortClearContents。我將UsedRange排序,但我不認爲是你想要的。您需要定義要排序的範圍,可以使用Excel中的名稱管理器或代碼中的命名範圍。

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 
    End If 
    'You need to define a range that you want sorted 
    'here I've used UsedRange 
    ActiveSheet.UsedRange.Sort Key1:=Range("B7"), Order1:=xlAscending, Header:=xlNo, _ 
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ 
        DataOption1:=xlSortNormal 

    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 
+0

嗨@Doug克蘭西,非常感謝你的這一點。這整理了錯誤信息,謝謝!,但不幸的是,它不會移動這些行,以便那些有數據的人坐在一起。可以告訴我,請你有任何想法?非常感謝和親切的問候 – IRHM 2013-02-09 15:35:35

+0

請參閱我的編輯。 – 2013-02-09 15:50:27

+0

嗨,非常感謝你。正如你所建議的那樣,我錄製了一個宏來對信息進行排序,但是我一定在錯誤的地方出錯了。我正在嘗試添加一個'Else If'到行:'RangeToClear.ClearContents,這樣如果行是空白的,行就會向上移動。當我運行此操作時,儘管我沒有收到任何錯誤消息,但排序不起作用,也沒有對錶單進行更改。我使用更新的代碼編輯了我的原始帖子。請你可以看看這個,請讓我知道我哪裏出了問題。非常感謝和親切的問候。 – IRHM 2013-02-09 15:56:50