Excel 2013:计算不同 sheet 上的单元格时,计算特定 sheet 的单元格会重置

Excel 2013: Cells with calculations on specific sheet reset when calculating cells on different sheet

Excel 2013

我在这个工作簿中有 3 个工作sheet,它是高度格式化的,我使用了我在 VBA 中编码的自定义公式,它利用了 Application.Volatile,所以它会自动刷新计算输入新数据的时间。

我的团队已经上下调整了此工作簿的格式,并创建了一个包含我们公司财务信息的巨大跟踪器。问题是,现在当我们打开工作簿并点击 f9/load 计算 sheet 函数时,只有 selected worksheet 会根据其参考更新和计算sheet 中的单元格。

它应该这样做,但问题出在其他两个选项卡中(请注意,总共有 3 个),具有公式的单元格将恢复为全零或当前不适用的旧数据.当您 select 最初未 selected 的其他两个选项卡之一并点击 f9/load 计算 sheet 函数时,具有曾经具有 zeros/old 的函数的单元格它们内部的数据根据​​单元格引用的新值进行更新,并且工作正常。

当我们切换选项卡并重新初始化 f9/calculate sheet 函数时,它一直这样做,当前未 selected 的其他两个选项卡重置并显示全零或旧数据。我一直在谷歌上搜索并到处寻找解决方案,但没有任何效果。

Function RedFinder(MyCellColumn As Integer, MyOffset As Integer, MonthCheck As Integer, YearCheck As Integer)
    Application.Volatile
'    Dim MyCellRow As Integer     'row I want to select
    Dim MyMoneyValue As Variant 'Single holds a decimal variable
    Dim MyAnswerString As String
'    Sheets("Sheet1").Activate  'activate sheet1 at cell script runs on
'    MyCellRow = 115              'set variable MyCellRow to row 1
    MyMoneyValue = CDec("0.0")


'    ActiveSheet.Cells(MyCellRow, MyCellColumn).Select    'select active cell based on input vars
    For MyCellRow = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row    'for loop used to go through all cells
        If IsDate(ActiveSheet.Cells(MyCellRow, MyCellColumn)) Then    'checks if cell is a date
            If Month(ActiveSheet.Cells(MyCellRow, MyCellColumn)) = MonthCheck And Year(ActiveSheet.Cells(MyCellRow, MyCellColumn)) = YearCheck Then    'checks if month and date match
                If IsNumeric(ActiveSheet.Cells(MyCellRow, MyCellColumn).Offset(0, MyOffset)) Then    'checks if corresponding column is a number
                    If ActiveSheet.Cells(MyCellRow, MyCellColumn).Offset(0, MyOffset).Font.Color = 255 Then    'checks if cell text color is red, 255 is the number Font.Color returns for RGB Red (255,0,0)
                        MyMoneyValue = MyMoneyValue + ActiveSheet.Cells(MyCellRow, MyCellColumn).Offset(0, MyOffset)    'adds cell value to MyMoneyValue
'                       MyAnswerString = MyMoneyValue
'                       MyCellRow = MyCellRow + 1
'                   Else
'                       MyCellRow = MyCellRow + 1
                    End If
                End If
'               Else
'               MyAnswerString = "False"
'               MyCellRow = MyCellRow + 1
            End If
        End If
    Next MyCellRow
'MsgBox MyCellColumnA
'RedFinder = Year(ActiveSheet.Cells(MyCellRow, MyCellColumn))
RedFinder = MyMoneyValue    'sets function to report total of MyMoneyValue
End Function

您需要删除所有 ActiveSheet 引用并将它们替换为对包含调用您的 UDF

的公式的 sheet 的引用
Function RedFinder(MyCellColumn As Integer, MyOffset As Integer, MonthCheck As Integer, YearCheck As Integer)
    Application.Volatile

    Dim MyMoneyValue As Variant 'Single holds a decimal variable
    Dim MyAnswerString As String
    Dim sht As Worksheet, c As Range, MyCellRow As Long

    Set sht = Application.Caller.Parent '<<<<    or use Application.ThisCell.Parent

    MyMoneyValue = CDec("0.0")

    For MyCellRow = 2 To sht.Cells(Rows.Count, 1).End(xlUp).Row
        Set c = sht.Cells(MyCellRow, MyCellColumn)
        If IsDate(c.Value) Then
            If Month(c.Value) = MonthCheck And Year(c.Value) = YearCheck Then    'checks if month and date match
                If IsNumeric(c.Offset(0, MyOffset)) Then
                    If c.Offset(0, MyOffset).Font.Color = 255 Then
                        MyMoneyValue = MyMoneyValue + c.Offset(0, MyOffset)
                    End If
                End If
            End If
        End If
    Next MyCellRow

    RedFinder = MyMoneyValue
End Function