Excel 2013:在计算不同工作表上的单元格时,在特定工作表上计算的单元格重置

Excel 2013

我在这个工作簿中有3个工作表,它的高度格式化,我用VBA编码的自定义公式,它利用Application.Volatile,所以它每次input新的数据时自动刷新计算。

我的团队已经格式化了这个工作簿,创build了一个包含我们公司财务数据的巨大追踪器。 问题是,现在当我们打开工作簿并按f9 /加载计算工作表函数时,只有选定的工作表将根据工作表内的参考单元更新和计算。

它应该这样做,但问题是在其他两个选项卡内(请注意,总共有3个),具有公式的单元格将恢复为当前不适用的全零或旧数据。 当你select另一个最初没有被选中的两个选项卡中的一个,并按f9 / load calculate sheet函数时,那些曾经具有零/旧数据的函数的单元格根据单元格引用的新值进行更新,工作正常。

它会继续这样做,因为我们切换标签并重新初始化f9 / calculate sheet函数,其他两个当前未选中的选项卡重置并显示全零或旧数据。 我一直在search和寻找解决scheme,没有任何工作。

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引用,并将其replace为包含调用您的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