Excel VBA - worksheet.calculate不会计算我的所有公式

时间:2014-09-05 13:42:50

标签: excel vba excel-vba

Private Sub Worksheet_Activate()

    If Worksheets("Input").Range("S5").Value = "Yes" Then
       MsgBox "Please make sure you've completed the historical deductible amounts for EL"
    End If

    Worksheets("Input").Calculate
    Worksheets("EL").Calculate
    Worksheets("PPL").Calculate
    Worksheets("Auto").Calculate
    Worksheets("AL").Calculate
    Worksheets("APD").Calculate
    Worksheets("Loss Layering").Calculate
    Worksheets("Sheet2").Calculate
    Worksheets("Premium").Calculate

End Sub

为了加速一个非常大的工作簿,我已经关闭了自动计算,并创建了一个层次结构,我在工作簿中移动时计算工作表。我的问题是,对于任何重型公式,例如 sumif sumproduct ,这些值不会在我的活动工作表中计算,它们会保持为零。我已经尝试过application.calculate和 CalculateFull ,这些都有效,但我发现它们占用了太多时间。我正试图找到一种方法来做到这一点,同时尽可能快速,简单和用户友好的模板。有什么建议吗?

3 个答案:

答案 0 :(得分:5)

目前尚不清楚哪个工作表是活动工作表并包含此代码,但我可以想出2个可能的问题原因。

1)您尚未在活动工作表上调用worksheet.calculate 2)由于worksheet.calculate忽略对其他工作表的依赖性,因此如果工作表上的公式始终引用已经计算过的其他工作表,则您使用的计算顺序将仅按您的要求工作。换句话说,工作表计算序列必须与工作表间参考序列完全匹配,并且必须没有任何正向工作表间参考(包括定义的名称等)。

作为一般性观点,我通常不会期望使用worksheet.calculate来计算整个工作簿会比使用Application.Calculate更快(虽然我确信有时它会更快)

答案 1 :(得分:1)

我知道它已经有一段时间了,但它已经困扰了我一段时间,我刚刚出来了一个解决方法。我创建了一个sub,当我想强制计算范围时,我调用它。

    /* CAPTCHA MENU CONTENTS */

        //"Account Creation" Text
            JLabel lblAccountCreation_3 = new JLabel("Account Creation");
            lblAccountCreation_3.setBounds(263, 89, 109, 16);
            panelCAPTCHA.add(lblAccountCreation_3);

        //"Please input the above message:" Text
            JLabel lblInputMessage = new JLabel("Please input the above message:");
            lblInputMessage.setBounds(207, 383, 209, 16);
            panelCAPTCHA.add(lblInputMessage);

        //Initiate CAPSLOCK Filter
            DocumentFilter filter = new UppercaseDocumentFilter();

        //CAPTCHA TextField
            CAPTCHAtextField = new JTextField();
            CAPTCHAtextField.setBounds(258, 411, 130, 26);
            panelCAPTCHA.add(CAPTCHAtextField);
            ((AbstractDocument) CAPTCHAtextField.getDocument()).setDocumentFilter(filter);

        //CAPTCHA Image Array
            Image[] CAPTCHAimageArray = {c2G4QH, c4FTD2, c7BJHL, c7JDFV, c9PB43, c9TVB4, cADVE8, cAZQRV,
                    cBLTFT, cBYF4D, cD8URH, cDBVFX, cDQAXC, cECD6A, cERTYA, cGTJRD, cGY67E, cHDP7R,
                    cJU4RV, cK8CRW, cKJPHL, cKMFDM, cL9MBP, cLGU3W, cLKMDR, cLMRTD, cLMUFX, cLPDT2,
                    cLPTY2, cLXF49, cMKNLH, cMY62A, cPT7W2, cRDAVH, cRTLPQ, cRVBAZ, cT7TMW, cUL4B7,
                    cUW2CZ, cVBCHY, cVF4TU, cW36X9, cWX2DT, cYT782, cYWRQZ, cZKGF8};

        //CAPTCHA String Array
            String[] CAPTCHAstringArray = {"2G4QH", "4FTD2", "7BJHL", "7JDFV", "9PB43", "9TVB4", "ADVE8", "AZQRV",
                    "BLTFT", "BYF4D", "D8URH", "DBVFX", "DQAXC", "ECD6A", "ERTYA", "GTJRD", "GY67E", "HDP7R",
                    "JU4RV", "K8CRW", "KJPHL", "KMFDM", "L9MBP", "LGU3W", "LKMDR", "LMRTD", "LMUFX", "LPDT2",
                    "LPTY2", "LXF49", "MKNLH", "MY62A", "PT7W2", "RDAVH", "RTLPQ", "RVBAZ", "T7TMW", "UL4B7",
                    "UW2CZ", "VBCHY", "VF4TU", "W36X9", "WX2DT", "YT782", "YWRQZ", "ZKGF8"};

        //CAPTCHA Image Generation
            Random rn = new Random();
            int i = rn.nextInt(46);
            JLabel lblCAPTCHA = new JLabel("");
            lblCAPTCHA.setBounds(162, 152, 300, 180);
            panelCAPTCHA.add(lblCAPTCHA);
            lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));

        //"Submit" Button
            JButton btnSubmit_4 = new JButton("Submit");
            btnSubmit_4.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String CAPTCHAinput = CAPTCHAtextField.getText();
                    //Check if input matches CAPTCHA
                    for (int j = 0; j <= 46; ++j) {
                        if (i == j) {
                            String CAPTCHA = CAPTCHAstringArray[i];
                        //System.out is to see which captcha the program is focusing on
                            System.out.println("i = " + i);
                            System.out.println("CAPTCHA is " + CAPTCHA);
                            if (compareCAPTCHA (CAPTCHA, CAPTCHAinput)) {
                                panelEmail.setVisible(true);
                                panelCAPTCHA.setVisible(false);
                                CAPTCHAtextField.setText("");
                                int i = rn.nextInt(46);
                                lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));
                            }
                            else {
                                JOptionPane.showMessageDialog(null, "Error - CAPTCHA input incorrect!");
                                CAPTCHAtextField.setText("");
                                int i = rn.nextInt(46);
                                lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));
                            }
                        }
                    }
                }
            });
            btnSubmit_4.setBounds(260, 463, 117, 29);
            panelCAPTCHA.add(btnSubmit_4);

        //"New CAPTCHA" BUTTON
            JButton btnNewButton = new JButton("New CAPTCHA");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int i = rn.nextInt(46);
                    lblCAPTCHA.setIcon(new ImageIcon(CAPTCHAimageArray[i]));
                }
            });
            btnNewButton.setBounds(255, 504, 130, 29);
            panelCAPTCHA.add(btnNewButton);

这相当于重新输入公式,从而强制重新计算。它不是很花哨,但它有效。

答案 2 :(得分:-2)

''simple solution, example:

呼叫重新计算(Sheet3。[A10:G10])

结束子 子重新计算(作为范围) rng.value = rng.Formula 结束