0

除了使用简单的函数之外,我在 VBA 中的第一次尝试;要求在这里开始:

假设这张(一张)表

factor      b-count c-count d-count
A2          b2      c2      d2      ...
A3          b3      c3      d3      ...

假设这些是第一列和第一行 A1 到 D3,每个都包含数值。

如果factoris 1,我希望 A(N) (列 'A', row N >= 2)保存第 1 行和第 N 行的 sumproduct。当factoris not时,扭曲就来了1。在那种情况下,我想要一个 sumproduct count*round(value * factor)

例子:

1.5         2       1       0       4
=myfunc(2)  4       8       11      15
=myfunc(3)  11      20      28      36
=myfunc(4)  29      53      74      94

其中 myfunc(2) 应导致
round(4*1,5)*2+round(8*1,5)*1+round(15*1,5)*4= 6*2+12*1+23*4= 12+12+92= 116、 myfunc(3) = 17*2+30+54*4= 34+30+216= 280、 myfunc(4) = 44*2+80+141*4= 88+80+564=732等。

我可以在每个值下面插入一行,将每个值乘以因子;但我会喜欢更高级的东西。

基本上认为(双关语不是故意的):

col='B'
sum=0
do while (col)(N)>0
    sum=sum+(col)(1)*round((col)(N)*A1;0)
    col=col+1
loop
A(n)=sum

其中 (col)(N) 指的是 columncol和 row中的单元格N

学习手册不够重要;但如果有人能即兴做这件事,那就太好了。

还有一点:我读过自定义函数必须存储在“标准库”中;
但我找不到任何关于如何做到这一点的提及。谁会给我指出正确的手册页?

4

1 回答 1

1

转到工具 -> 宏 -> 组织宏 -> OpenOffice Basic。选择My Macros -> Standard-> Module 1(这就是Standard库的含义),然后按Edit

粘贴以下代码。

Function SumProductOfTwoRows(firstColumn As Long, row As Long, firstRow As Long)
    'For example: =SUMPRODUCTOFTWOROWS(COLUMN(); ROW(); ROW($A$1))
    firstColumn = firstColumn - 1  'column A is index 0
    row = row - 1  'row 1 is index 0
    firstRow = firstRow - 1  'row 1 is index 0
    oSheet = ThisComponent.CurrentController.ActiveSheet
    sum = 0
    column = firstColumn + 1
    factor = oSheet.getCellByPosition(firstColumn, firstRow).getValue()
    Do
        value = oSheet.getCellByPosition(column, row).getValue()
        count = oSheet.getCellByPosition(column, firstRow).getValue()
        If value = 0 Then Exit Do
        sum = sum + count * CLng(value * factor)
        column = column + 1
    Loop
    SumProductOfTwoRows = sum
End Function

在 A2 中输入此公式并拖动以填充到 A4。

=SUMPRODUCTOFTWOROWS(COLUMN(); ROW(); ROW($A$1))

结果:

116、280、732

这种用户定义的函数在重新打开文件时会产生错误。为避免该错误,请参阅我在https://stackoverflow.com/a/39254907/5100564上的回答。

于 2018-02-08T17:21:54.607 回答