复制列中的所有单元格

Pra*_*ike 0 excel vba excel-2007 excel-vba

我有一张有200行的纸张,其间有一些空白.

我不能使用End(xlDown)End(xlUp)获取整个范围,因为有空行.

如何复制到列中包含数据的最后一个范围?

另外,我不想复制标题.

请建议我如何在Excel VBA中执行此操作.

Sid*_*out 8

复制它们的最佳方法是使用自动过滤器并在空白上过滤它们,然后复制可见范围

例如

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim rngToCopy As Range, rRange As Range

    Set ws = Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        Set rRange = .Range("A1:A" & lRow)

        '~~> Remove any filters
        .AutoFilterMode = False

        With rRange 'Filter, offset(to exclude headers) and copy visible rows
            .AutoFilter Field:=1, Criteria1:="<>"
            Set rngToCopy = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
        End With

        '~~> Remove any filters
        .AutoFilterMode = False

        rngToCopy.Copy

        '
        '~~> Rest of the Code
        '
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)