计算的自动修改

时间:2015-07-27 09:17:01

标签: excel vba excel-vba

我创建了一个代码,允许我将不同工作表中不同单元格中的(合并)数字合并为一张工作表(合并工作表中包含总数 - 称为工作表0)

然后我创建了一个按钮来激活这些代码,它可以工作。

我想知道的是什么: 是每次我修改我的任何单元格中的数字时,它会直接修改我的最终结果(在sheet0上),而不必再次按下我的按钮。

这是与我的按钮相关联的代码:

Sub Consolidate()
Application.Calculation = xlCalculationAutomatic 'I thought this line would      help me 
Dim Sheet As Worksheet
Dim Consolidated As Long
For y = 2 To 152
For x = 2 To 14
    Consolidated = 0
    For Each Sheet In Sheets

        If Right(Sheet.Name, 5) = "E2016" Then

            If IsNumeric(Sheet.Cells(y, x).Value) Then
             Consolidated = Consolidated + CLng(Sheet.Cells(y, x).Value)
             Sheet0.Cells(y, x) = Consolidated
            End If

            If Not IsNumeric(Sheet.Cells(y, x).Value) Then
                Sheet0.Cells(y, x) = " "
            End If

            If IsEmpty(Sheet.Cells(y, x).Value) Then
                 Sheet0.Cells(y, x) = " "
            End If

       End If
   Next
Next
Next
End Sub

3 个答案:

答案 0 :(得分:1)

根据评论中的建议,每次更改任何工作表时,都可以使用Change Event运行代码。但这可能会使您的Excel速度变慢,因为 - 每次更改后 - 您的代码都必须运行。

所以,我建议在condolidated表上使用事件Worksheet_Activate。因此,每次实际打开Sheet0以查看合并数字时,您的代码都会运行。

为了实现这一点,请进入VBE并双击Sheet0以查看该工作表的代码。然后插入以下代码:

Private Sub Worksheet_Activate()

Call Consolidate

End Sub

根据您的评论,您的问题/目标似乎已发生变化。为了在Sheet0上获得这个动态公式,您可能需要尝试以下代码(没有保修,因为我无法在没有实际工作表的情况下对其进行测试):

If IsNumeric(Sheet.Cells(y, x).Value2) Then
    If Len(Sheet0.Cells(y, x).Formula) = 0 Then
        Sheet0.Cells(y, x).Formula = "=SUM('" & Sheet.Name & "'!" & Sheet.Cells(y, x).Address & ")"
    Else
        Sheet0.Cells(y, x).Formula = Left(Sheet0.Cells(y, x).Formula, Len(Sheet0.Cells(y, x).Formula) - 1) & ",'" & Sheet.Name & "'!" & Sheet.Cells(y, x).Address & ")"
    End If
End If

由于我没有你的表,我无法测试它。希望它按原样工作。如果没有,至少你明白了。

答案 1 :(得分:0)

如果我理解正确,你可以在SUM中使用3维参考,例如:

= SUM(Sheet2:Sheet8!A1)或= SUM(Sheet2:Sheet8!A1:Z99)

答案 2 :(得分:0)

嗯,我觉得自己很愚蠢,但我有一个问题:这是我的公式(对Ralph,上层的Credits)适用于我的所有细胞而不是第一个B:2。在这一个没有价值,它的公式以这种形式显示:  ,'阿联酋E2016'!$ B $ 2,' KSA E2016'!$ B $ 2,'塞浦路斯E2016'!$ B $ 2,' QATAR E2016&# 39;!$ B $ 2)

在其他单元格中,这里是公式: = SUM(' LEB E2016'!$ B $ 4,'阿联酋E2016'!$ B $ 4,' KSA E2016'!$ B $ 4,'塞浦路斯E2016'!$ B $ 4,' QATAR E2016'!$ B $ 4)但如果我不点击单元格,则会显示该值。 这是我需要的,它在任何地方工作,但不是在我的第一个单元格。

它使我疯狂。我试图用断点解决它,但无所事事,我不明白。对不起所有这些问题,但我在自己的2周内开始了VBA而且我还没有掌握它!这是公式:

 Sub Consolidate()

 Dim Sheet As Worksheet
'Dim Consolidated As String
 For y = 2 To 152
 For x = 2 To 14
 'Consolidated = ""
    For Each Sheet In Sheets

        If Right(Sheet.Name, 5) = "E2016" Then

            If IsNumeric(Sheet.Cells(y, x).Value) Then
             'Consolidated = Consolidated + "+" + Sheet.Cells(y, x).Formula
             'Sheet0.Cells(y, x) = Consolidated
                    If Len(Sheet0.Cells(y, x).Formula) = 0 Then
                    Sheet0.Cells(y, x).Formula = "=SUM('" & Sheet.Name & "'!" & Sheet.Cells(y, x).Address & ")"
                   Else
                    Sheet0.Cells(y, x).Formula = Left(Sheet0.Cells(y, x).Formula, Len(Sheet0.Cells(y, x).Formula) - 1) & ",'" & Sheet.Name & "'!" &    Sheet.Cells(y, x).Address & ")"
                    End If
            End If


            If Not IsNumeric(Sheet.Cells(y, x).Value) Then
                Sheet0.Cells(y, x) = " "
            End If

            If IsEmpty(Sheet.Cells(y, x).Value) Then
                Sheet0.Cells(y, x) = " "
            End If

        End If
    Next
Next
Next
End Sub