Excel VBA - 仅复制和粘贴可见表行

时间:2013-04-16 14:29:53

标签: excel vba excel-vba

我试图将表中的可见行复制到同一工作簿中的单独工作表中。我使用'ListObject'方法来处理表有点新鲜(出于几个原因,就模块的其余部分而言,直接引用表是一种更好的方法)

以下是我最好的尝试,当我运行它时,'Sheets("Sheet8").Range("A1").Paste'行上出现'运行时错误'438'',我已经在互联网上搜索了一个小时,现在试图找出我的内容我做错了,我怎么需要重新短语,以便将复制的数据粘贴到另一张表/表中?任何帮助将不胜感激!

谢谢,

亚当

Private Sub CopyVisibleAreaOfTable(ByVal TableName As String)

Const FN_NAME As String = "CopyVisibleAreaOfTable"
On Error GoTo catch

    Dim TargetTable As ListObject
    Dim NumberOfAreas As Long

    Set TargetTable = Sheets("Adj1").ListObjects(TableName)

    ' Check that there are fewer than 8192 seperate areas
    With TargetTable.ListColumns(1).Range
        NumberOfAreas = .SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
        Debug.Print NumberOfAreas
    End With

    If NumberOfAreas = 0 Then
        'Do something to trigger an error message
    Else

        TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy
        Sheets("Sheet8").Range("A1").Paste
        Application.CutCopyMode = False

    End If

finally:
    Exit Sub

catch:
    Call ErrorReport(FN_NAME, True, Err.Number, Err.Description, "Table Name: " & TableName)
    Resume finally

End Sub

1 个答案:

答案 0 :(得分:10)

将目标指定为.Copy方法的一部分:

TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy _
    Destination:=Sheets("Sheet8").Range("A1")