0

我正在执行一个 VB 函数来对不同类别的指定时间段内的累积金额进行排序。目标是人事账户费用。

例如,我在 A 列中的工作表“tmp”中有一些日期,在 B 列中有一个类别(例如薪水,运输......),在 C 列中有值。

我想定义一个函数,我试图设置:

Function Cumulatif(Categorie As String, Debut As Date, Fin As Date) As Double
    ' Do the sum of a category between a starting and ending date specified 
    Dim Operations As Variant
    Dim SpecificSum As Double
    Dim i As Integer
    Operations = ThisWorkbook.Sheets("tmp").Range("A1:C3")
    For Each Row In Operations.Rows
        SpecificSum = 0
    Next
    Cumulatif = SpecificSum
End Function

但我真的不知道如何从另一张表中获取值并在范围内进行循环以设置此总和。有人可以帮我吗?

4

1 回答 1

0
  1. 操作的分配需要是对象分配,意思是:使用 Set(不是 Let/default)。

    设置操作 = ThisWorkbook.Sheets("tmp").Range("A1:C3)

  2. 您可以从另一张表中获取值(假设您的意思是“tmp”),如下所示

    SpecificSum = SpecificSum + Row.Cells(1, 1)
    

我认为你最好让函数接受一个额外的范围参数并提供 Sheet1!A 作为参数;这样 Excel 就会知道何时需要重新计算。

Function Cumulatif(Categorie As String, Debut As Date, Fin As Date, Operations As Range) As Double
    ' Do the sum of a category between a starting and ending date specified
    'Dim Operations As Variant
    Dim SpecificSum As Double
    Dim i As Integer
    'Set Operations = ThisWorkbook.Sheets("Sheet1").Range("A2:C3")
    For Each Row In Operations.Rows
        SpecificSum = SpecificSum + Row.Cells(1, 1)
    Next
    Cumulatif = SpecificSum
End Function

并使用这样的公式调用 Cumulatif:

=Cumulatif("hello9","10/1/2010", "10/31/2010",Sheet1!A2:A3)
于 2012-12-08T23:10:07.673 回答