0

我正在尝试使用以下代码将一些数据复制到不同的工作表:

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

只有当我尝试这样做时,我才会收到以下错误:编译错误:未定义子或函数(此错误指向 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

4 回答 4

0

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

所以接下来的问题是:

  1. 你创建了一个Function被调用的lastRow(args1)
  2. 你真的创造了 aFunction而不是偶然 aSub吗?
  3. 是否Function存在于同一个Module中,如果不存在Public(因此其他模块可以使用它)?
于 2013-10-10T08:24:21.843 回答
0

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

而不是你的线:

Lr = lastRow(DestSheet)

把这个:

Lr = DestSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
于 2013-10-10T08:49:16.350 回答
0

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

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

结束子

于 2013-10-10T10:20:23.147 回答
0

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

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

为了优雅的展示,请查看各种示例。请特别参阅他部分的底部,了解您需要在代码中包含的函数的说明(例如“LastRow 函数”等 - 代码中的注释指的是此函数......)。

我引用 :

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

于 2014-11-26T12:31:09.873 回答