我已经获得了一组数据,这些数据已经过自动过滤以符合某些标准。我要将这个数据子集粘贴到一个新工作表中,所以它本身就是这样,然后将其从主工作表中删除。
除了最后一部分之外,我已经完成了大部分工作,将其从主工作表中删除。主要的问题是我需要保留主表的标题行,并且由于这些结果是自动过滤的,所以我不能指望第二行是2.我已经尝试了许多不同的事情,在我将我需要的东西复制到第二张纸之后,最新的抵消了第二行。这就是我到目前为止所做的事情(是的,有些东西已被评论用于测试,我不需要使用宝贵的秒钟做我认识的工作)
Sub copyToN2r()
'
' copyton2r Macro
'
'
Dim Lr As Long
Dim start As Long
Lr = Range("A" & Rows.Count).End(xlUp).Row
'Range("A" & Lr).SpecialCells(xlCellTypeVisible).Copy
Destination:=Sheets("N2R Register").Range("A1")
'Range("A1:F1").Copy
Range("A1").Offset(1, 0).SpecialCells(xlCellTypeVisible).Clear
'Range("A1").Paste
End Sub
答案 0 :(得分:0)
查看下面的代码段是否适合您 - 评论内联:
Sub copyToNewSheet()
' data that will be copied
Dim dataWithHeader As Range
Dim dataOnly As Range
Set dataWithHeader = [Sheet3!a1].CurrentRegion
With dataWithHeader
Set dataOnly = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
End With
' so now you copy data and the header
dataWithHeader.Copy [Sheet4!a1]
' and delete copied visible rows
' you can't iterate throw rows and delete those rows at the same time
' as it will cause issues. We first need to store all the ranges we'll delete
' in a collection -- and then iterate through that collection
Dim rw As Range
Dim cRowsToDel As New Collection
For Each rw In dataOnly.SpecialCells(xlCellTypeVisible).Rows
cRowsToDel.Add rw
Next
For Each rw In cRowsToDel
rw.Delete xlShiftUp
Next
' remove autofilter to see the results
If [Sheet3].AutoFilterMode Then [Sheet3].ShowAllData
End Sub
使用列标题测试数据集,过滤掉第2行和第3行(粘贴在A1中):
col1 col2 col3
data11 data21 data31
data12 data22 data32
data13 data23 data33
data14 data24 data34
data15 data25 data35