【问题标题】:Excel 2013: Cells with calculations on specific sheet reset when calculating cells on different sheetExcel 2013:计算不同工作表上的单元格时重置特定工作表上计算的单元格
【发布时间】:2017-02-12 00:16:07
【问题描述】:

Excel 2013

我在此工作簿中有 3 个工作表,其高度格式化,并且我使用了我在 VBA 中编写的自定义公式,该公式利用 Application.Volatile,因此每次输入新数据时它都会自动刷新计算。

我的团队对这本工作簿进行了上下格式化,并创建了一个巨大的跟踪器,其中包含我们公司的财务数据。问题是,现在当我们打开工作簿并按 f9/加载计算工作表功能时,只有选定的工作表会根据该工作表中的参考单元格进行更新和计算。

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

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

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

【问题讨论】:

  • 您的 VBA 函数(此处未包含)可能存在问题,因此将其添加到您的问题中会很有用。您可能依赖默认工作表引用,而不是使用工作表限定所有范围/单元格引用。或者,如果您使用的是Application.Evaluate(或只是Evaluate),那么它将始终默认为 Activesheet,即使它是从其他工作表中调用的。
  • 您使用 Application.Volatile 的事实告诉我,您的 UDF 可能有问题。尝试重写您的 UDF,以便 VBA 引用的所有单元格都包含在传递给 UDF 的 UDF 参数中 - 然后您将不再需要 Application.volatile。
  • 还要注意,即使我删除了 application.volatile,上面讨论的问题仍然存在。
  • 我还想指出,工作表没有以任何方式通过单元格引用连接。

标签: vba excel excel-formula excel-2013


【解决方案1】:

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

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

【讨论】:

  • 你是最好的蒂姆。非常感谢。因此,据我了解,activesheet 对多张纸的情况不利。正如您所说,最好参考包含调用 UDF 的公式的工作表。所以这个 sht = Application.Caller.Parent 让我调用它运行的单元格表而不是我选择的活动表?
  • 是的 - 这是正确的。 ActiveSheetSelectActivateActiveCell 等都是理想情况下仅在绝对必要时才会出现在代码中的关键字。
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多