Excel VBA:SpecialCells复制和粘贴可见单元格不起作用

时间:2017-12-08 19:50:05

标签: excel vba excel-vba

我有一个vba代码,允许用户选择一个范围并将其删除,但在删除之前,它会复制所选范围并将其粘贴到另一个工作表中。

到目前为止,我设法使用我在其他地方找到的简洁代码并编辑以满足我的需求,但我发现代码不能很好地处理过滤后的行。我设法只删除过滤的行,但我无法复制和粘贴过滤的行。

如何仅复制已过滤的行并将其粘贴到以A4开头的RowsDeleted表格的最后一行或第一行?当我尝试删除的所有行都是过滤范围内的2行时,我对PasteSpecial的行数给了我8行。

现在它给我一个粘贴/范围方法失败。

'Error Handling
  If DeleteRng Is Nothing Then GoTo InvalidSelection
  If DeleteRng.Address = ActiveCell.ListObject.DataBodyRange.Address Then GoTo DeleteAllRows
  If ActiveCell.ListObject.DataBodyRange.Rows.Count = 1 Then GoTo DeleteOnlyRow

'Ask User To confirm delete (since this cannot be undone)
    DeleteRng.Select

    If DeleteRng.Rows.Count = 1 And DeleteRng.Areas.Count = 1 Then
      Answer = MsgBox("Are you sure you want to delete the currently selected table row? " & _
       " This cannot be undone...", vbYesNo, "Delete Row?")
    Else
      Answer = MsgBox("Are you sure you want to delete the currently selected table rows? " & _
       " This cannot be undone...", vbYesNo, "Delete Rows?")
    End If

'Delete row and copy in another sheet (if wanted)
  If Answer = vbYes Then
  On Error GoTo InvalidCopySelection
  Set copyRange = DeleteRng.SpecialCells(xlCellTypeVisible)
  copyRange.SpecialCells(xlCellTypeVisible).Copy
  Sheets("RowsDeleted").Range("A4:A4").Insert Shift:=xlDown
  Sheets("RowsDeleted").Range("A4").SpecialCells(xlCellTypeVisible).PasteSpecial xlPasteValues
  DeleteRng.SpecialCells(xlCellTypeVisible).Delete
  End If


  End If

0 个答案:

没有答案