Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Excel自定义函数更新不正确_Vba_Excel_Excel 2007_User Defined Functions - Fatal编程技术网

Vba Excel自定义函数更新不正确

Vba Excel自定义函数更新不正确,vba,excel,excel-2007,user-defined-functions,Vba,Excel,Excel 2007,User Defined Functions,我试图编写(我的第一个)自定义函数,该函数将基本上通过一个报告循环,以汇总未来1、3、6个月(等)的机器成本 函数返回值,但是,这些值不正确,更麻烦的是,在包含自定义函数的单元格上双击会更改使用自定义函数的周围单元格的值 我处理的代码如下: Option Explicit Dim months As Integer 'a month is considered as 30 days Dim cost As Long Dim FleetData As Range Dim rowCounter A

我试图编写(我的第一个)自定义函数,该函数将基本上通过一个报告循环,以汇总未来1、3、6个月(等)的机器成本

函数返回值,但是,这些值不正确,更麻烦的是,在包含自定义函数的单元格上双击会更改使用自定义函数的周围单元格的值

我处理的代码如下:

Option Explicit

Dim months As Integer 'a month is considered as 30 days
Dim cost As Long
Dim FleetData As Range
Dim rowCounter As Long
Dim lastRow As Long
Dim firstRow As Long
Dim component As Range
Dim dateOfAction As Range
Dim totalApprox As Range
Dim dateHorizon As Date 'Date to which the user wants to total the maintenance cost for

Private Function totalCosts(xMonths As Range)
'Dim totalCosts As Long
Application.Volatile

dateHorizon = Date + (30 * months)

firstRow = [A10].Row
rowCounter = firstRow
lastRow = Range("A65000").End(xlUp).Row
Set FleetData = [A10:S14]

If IsNumeric(xMonths.Value) Then months = xMonths.Value Else
If IsDate(xMonths.Value) Then months = (xMonths.Value - Date) / 30
cost = 0
    Do While rowCounter < lastRow
        Set component = Range(Cells(rowCounter, 1), Cells(rowCounter, 19))
        Set dateOfAction = Cells(rowCounter, 7)
        Set totalApprox = Cells(rowCounter, 12)
        If dateOfAction <= dateHorizon Then
            cost = cost + totalApprox
        End If
        totalCosts = cost
        rowCounter = rowCounter + 1
    Loop

End Function
单击单元格似乎会左右移动值,但没有可识别的顺序

我在谷歌上搜索了一下,但到目前为止,似乎没有什么能解决这个问题


任何帮助都将不胜感激

一些提示和需要检查的内容:

  • 不要使用模块范围的变量(除非您需要与其他模块共享这些变量。即使这样,通常也有更好的方法)
  • 除非您真的需要,否则不要使用Volatile(我认为在这种情况下您不会这样做)
  • 务必将所有范围作为参数传递到函数中
  • 如果必须使用直接范围引用,请记住,非限定引用,如
    range(…
    Cell(…
    )引用的是活动工作表上的范围。如果数据表不活动,此UDF将返回意外结果。请使用类似
    工作表(“Sheet1”)。范围(…
    相反。但我再说一遍,将范围引用作为参数传递到函数中要好得多
  • 在设置变量之前,代码引用变量
    months
  • 如果从单元格中调用公式中的自定义项,尝试设置单元格值(如
    totalCosts=cost
    )将不起作用。您提供的链接中已明确说明了这一点(第一点是,您不能直接创建VBA自定义项:列表)。另一方面,如果您从
    子系统调用UDF,并将该子系统作为宏运行,它将正常工作

  • 如果您提供数据布局的完整细节以及如何使用UDF,我可以提供更具体的建议。

    我意识到我的代码规划得有多么糟糕。感谢您的提醒。我试图在UDF中添加更多内容,并且在我真正需要宏时尝试使用函数。非常感谢headsup,并为la道歉答复
    DateOfAction totalApprox 
    5/07/2014    $30,068.62 
    24/05/2005   $6,300.00 
    5/07/2012    $29,742.00
    5/07/2012    $4,360.28
    27/12/2012   $5,555.89