用于复制和粘贴列的最后一行的最后数据的函数

时间:2015-05-29 07:04:40

标签: excel vba excel-vba excel-formula excel-2010

下面是我复制和粘贴列最后一行数据的代码。它适用于命令按钮,但如果我将其作为一个功能,它不起作用。它给出0或错误的结果。请检查。感谢。

    Function CopyCell()

    Application.Volatile True
    Dim eRow As Integer
    Dim i As Integer

    With Sheets("Weekly Score")
    eRow = .Cells(Rows.Count, "N").End(xlUp).Row
    .Cells(eRow, "N").Copy
    .Range("AG3").PasteSpecial xlPasteValues
    End With

    ENd Function

2 个答案:

答案 0 :(得分:0)

尝试这样做,将MyFunction分配给按钮

Option Explicit
Sub MyFunction()
   Call CopyCell
End Sub

Function CopyCell()
Application.Volatile True
    Dim eRow    As Integer
    Dim i       As Integer

    With Sheets("Weekly Score")
        eRow = .Cells(Rows.count, "N").End(xlUp).Row
        .Cells(eRow, "N").copy
        .Range("AG3").PasteSpecial xlPasteValues
    End With

End Function

答案 1 :(得分:0)

如果你想要一个UDF修改这个函数:

Public Function CopyCell()
    Application.Volatile True
    Dim eRow As Integer
    Dim i As Integer

    With Sheets("Weekly Score")
      eRow = .Cells(Rows.Count, "N").End(xlUp).Row
      CopyCell =  .Cells(eRow, "N").Value
    End With
End Function

否则会丢失Volatile并将Function更改为“Sub”并简单地手动调用它:

Sub CopyCell()

    Dim eRow As Integer
    Dim i As Integer

    With Sheets("Weekly Score")
     eRow = .Cells(Rows.Count, "N").End(xlUp).Row
     .Cells(eRow, "N").Copy
     .Range("AG3").PasteSpecial xlPasteValues
    End With
End Sub

请记住,Application.Volatile True将导致函数在执行任何计算时重新计算。这可能会严重影响绩效。