试图将数据值复制到另一张表Lastrow错误

时间:2013-10-10 08:00:19

标签: excel vba copy paste

我正在尝试使用以下代码将一些数据复制到另一个表:

Sub FilterButton()
    Dim SourceRange As Range, DestRange As Range
    Dim DestSheet As Worksheet, Lr As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'fill in the Source Sheet and range
    Set SourceRange = Sheets("Imported Data").Range("A1:K1")

    'Fill in the destination sheet and call the LastRow
    'function to find the last row
    Set DestSheet = Sheets("Test")
    Lr = lastRow(DestSheet)

    'With the information from the LastRow function we can
    'create a destination cell
    Set DestRange = DestSheet.Range("A" & Lr + 1)

    'Copy the source range and use PasteSpecial to paste in
    'the destination cell
    SourceRange.Copy
    DestRange.PasteSpecial _
            Paste:=xlPasteValues, _
            operation:=xlPasteSpecialOperationNone, _
            skipblanks:=False, _
            Transpose:=False
    Application.CutCopyMode = False

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

只有当我尝试这样做时,才会出现以下错误:编译错误:Sub或Function未定义(此错误指向lastRow)...我该如何解决此问题?

编辑:

   Sub FilterButton()
    Dim SourceRange As Range, SRange, DestRange, myMultipleRange As Range
    Dim DestSheet As Worksheet, Lr As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'fill in the Source Sheets and ranges
    Set SourceRange = Sheets("Imported Data").Range("A2:B:C")
    Set SRange = Sheets("Imported Data").Range("E2:E8")
    Set myMultipleRange = Union(SourceRange, SRange)

    'Fill in the destination sheet and find the last known cell
    Set DestSheet = Sheets("Test")

    'With the information on the new sheet
    Set DestRange = DestSheet.Range("A:B:C:E")

    'Copy the source range and use PasteSpecial to paste in
    'the destination cell
    myMultipleRange.Copy
    DestRange.PasteSpecial _
            Paste:=xlPasteValues, _
            operation:=xlPasteSpecialOperationNone, _
            skipblanks:=False, _
            Transpose:=False
    Application.CutCopyMode = False

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

我想要的这些范围,但我无法在多项选择中使用该范围:(!

4 个答案:

答案 0 :(得分:0)

这个功能真的存在吗?它不是VBA中的默认函数,因此您必须创建自己的函数!

接下来的问题是:

  1. 您是否创建了名为Function的{​​{1}}?
  2. 你真的创建了一个lastRow(args1)而不是偶然的Function吗?
  3. Sub是否存在于同一Function中,如果不存在Module(其他模块可以使用它)?

答案 1 :(得分:0)

您的LastRow功能可能会有很多替代品。您可以尝试使用此选项:

而不是你的行:

Lr = lastRow(DestSheet)

把这一个:

Lr = DestSheet.Cells.SpecialCells(xlCellTypeLastCell).Row

答案 2 :(得分:0)

Sub FilterButton()     昏暗的SourceRange作为范围,DestRange作为范围     Dim DestSheet As Worksheet,Lr As Long     Dim ColumnRange作为范围

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

'fill in the Source Sheet and range
Set SourceRange = Sheets("Imported Data").Range("A:C,E:E")

'Fill in the destination sheet and find the last known cell
Set DestSheet = Sheets("Test")

'With the information on the new sheet
Set DestRange = DestSheet.Range("A:E")

'Copy the source range and use PasteSpecial to paste in
'the destination cell
SourceRange.Copy
DestRange.PasteSpecial _
        Paste:=xlPasteValues, _
        operation:=xlPasteSpecialOperationNone, _
        skipblanks:=False, _
        Transpose:=False
Application.CutCopyMode = False

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Sub

答案 3 :(得分:0)

刚刚看到你的问题,我知道有点迟了。我不确定你是否知道,但你使用的代码是Ron de Bruin在他的链接中给出的例子的精确复制品

http://www.rondebruin.nl/win/s3/win001.htm

为了优雅的阐述,请看一下各种例子。 请参阅他的部分底部,了解您需要在代码中包含的函数的解释(例如'LastRow函数'等 - 代码中的注释引用此函数...)。

我引用:

重要说明:宏示例使用一个函数或多个函数,您可以在本页的最后一节中找到这些函数。不要忘记将工作簿中的函数复制到工作簿的标准模块中,如果刚开始使用VBA,请参阅此页面。 我在哪里粘贴我在互联网上找到的代码