复制动态范围

时间:2017-08-18 09:58:03

标签: excel vba excel-vba

我正在尝试将数据从动态(不断变化的)电子表格中的列复制到另一个电子表格中。原件也有我不需要的数据,我需要将其删除。复制和粘贴部分很简单,但是,当我录制它然后运行它时,我收到错误。

Sub BOS()

Sheets("BOX").Select
Range("N17").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.SpecialCells(xlCellTypeBlanks).Select
Application.CutCopyMode = False
Selection.EntireRow.Delete
ActiveSheet.Range("$A$3:$S$61").AutoFilter Field:=1, Criteria1:=Array("NA", "These will be listed in the FDO Report- see notes.", "will be included in the FDO Report"), Operator:=xlFilterValues
Rows("6:52").Select
Selection.Delete Shift:=xlUp
ActiveSheet.Range("$A$3:$S$56").AutoFilter Field:=1
Range("L25").Select
End Sub

错误截图:enter image description here

2 个答案:

答案 0 :(得分:0)

对我来说这是错误的,因为它没有找到任何空白单元格。最简单的解决方案是在不过多改变代码的情况下更换这些代码:

Selection.SpecialCells(xlCellTypeBlanks).Select
Application.CutCopyMode = False
Selection.EntireRow.Delete

有了这个:

Application.CutCopyMode = False

If Application.WorksheetFunction.CountBlank(Selection) > 0 Then
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete
End If

答案 1 :(得分:0)

Sub BOS()
    With Sheets("BOX")
        .Range(.Range("N17"), .Range("N17").End(xlDown)).Copy
    End With

    Sheets("Sheet2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

    On Error Resume Next
        Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0

    Range("$A$3:$S$61").AutoFilter Field:=1, Criteria1:=Array("NA", "These will be listed in the FDO Report- see notes.", "will be included in the FDO Report"), Operator:=xlFilterValues
    Rows("6:52").Delete Shift:=xlUp
    Range("$A$3:$S$56").AutoFilter Field:=1
End Sub
相关问题