复制一列中的所有单元格

我有一张有200行,中间有一些空白的表格。

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

我怎样才能复制到列中有数据的最后一个范围?

另外,我不想复制标题。

请build议我如何在Excel VBA中做到这一点。

复制它们的最佳方法是使用自动筛选器,并在空白处进行筛选,然后复制可见范围

例如

 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 

您可以使用下面的代码获取活动工作表A列中最后使用的行(如果在列中间有空白单元格,它也可以工作) – 如果需要在不同的工作表/列上运行,则根据需要进行修改。

 With ActiveSheet lastRow = .Range("A" & .Rows.Count).End(xlUp).Row .Range("A2:A" & lastRow).Copy 'copies column A used range, excluding header row End With