通过sub而不是电子表格将单元格范围传递给VBA函数

时间:2016-07-31 12:21:24

标签: excel vba excel-vba

我正在编写一个为我运行一些付款计算的函数。我所做的是我饲养细胞。

Function clinkmeup(Tuesdays As Excel.Range, Sundays As Excel.Range, Subbing As Excel.Range, Optional Other As Exce.Range = 0) As Variant 

e.g。 Payment(A10:A12, B12:B14, C13:C17)它会从这些单元格计算我的工资。当我从sub中调用它时,此函数正常工作:

Sub lmao()
    money = clinkmeup(Worksheets("Talent").Range("C9:C10"), Worksheets("Talent").Range("F9:F11", Worksheets("Talent").Range("H9:H12"))
    MsgBox (money)
End Sub

但是当我尝试从电子表格中调用它时(通过编写=clinkmeup(C9:C10, F9:F11, H9:H12))我收到了一个VALUE错误。

我已经在网上搜索了一个解决方案,但我没有发现任何与我自己的情况相关的内容。

这让我感到很沮丧,因为我是VBA的新手,任何帮助都会非常感激。

以下是我的全部代码:

    Function clinkmeup(Tuesdays As Excel.Range, Sundays As Excel.Range, Subbing As Excel.Range, Optional Other As Excel.Range = 0) As Variant
    Dim teaching As Double
    Dim admin As Double
    Dim seminar As Double
    Dim elem As Variant
    Dim sumo1 As Double


    teaching = Worksheets("Talent").Range("Teacrate")
    admin = Worksheets("Talent").Range("Admrate")
    seminar = Worksheets("Talent").Range("Semrate")
    sum1 = 0



    For Each elem In Tuesdays
        sum1 = sum1 + elem
    Next elem

    For Each elem In Sundays
        sum1 = sum1 + elem
    Next elem

    For Each elem In Subbing
        sum1 = sum1 + elem
    Next elem


    sum1 = (sum1/ 60) * teaching
    sum2 = 0



    If Not Other Is Nothing Then
        For Each elem In Other
             If elem.Offset(0, 1) = "S" Then
                 sum2 = (elem / 60) * seminar + sum2
             Else
                 sum2 = (elem / 60) * admin + sum2
             End If

         Next elem
    End If


    clinkmeup = sum1 + su2

   End Function

1 个答案:

答案 0 :(得分:1)

我复制了错误并通过替换函数头的这一部分来解决它:

Optional other As Range = Nothing

用这个:

{{1}}