2013-02-09 70 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