公式计算太慢了

问题描述 投票:0回答:2

我想写一个Excel宏:我有一些带有起始值的计算。有超过100个不同的起始编号,我想自动复制正确单元格中的起始值,其他一些单元格具有读取此值并使用它的公式。这些公式化器从网络(afaik)获取一些数据,在其他一些工作表中进行计算,并且计算需要几秒钟。我工作的纸张中的单元格只是链接到其他纸张中的其他单元格,因此公式不直接位于“工作表”中(表单#1)。我不知道这些公式究竟做了什么,它们不是来自我。对于每个起始编号,我想将结果复制到另一个工作表中。

Private Sub CommandButton1_Click()
Application.Calculation = xlCalculationManual

For i = 2 To Sheets(2).Range("A1048576").End(xlUp).Row
    Sheets(1).Cells(5, 3).Value = Sheets(2).Cells(i, 3).Value

    Application.CalculateFull

    Sheets(2).Cells(i, 4).Value = Sheets(1).Cells(7, 3).Value
    Sheets(2).Cells(i, 5).Value = Sheets(1).Cells(8, 3).Value
    Sheets(2).Cells(i, 6).Value = Sheets(1).Cells(9, 3).Value
    Sheets(2).Cells(i, 7).Value = Sheets(1).Cells(10, 3).Value
    Sheets(2).Cells(i, 8).Value = Sheets(1).Cells(11, 3).Value
    Sheets(2).Cells(i, 9).Value = Sheets(1).Cells(12, 3).Value
    Sheets(2).Cells(i, 10).Value = Sheets(1).Cells(15, 3).Value
    Sheets(2).Cells(i, 11).Value = Sheets(1).Cells(16, 3).Value
Next i

Application.Calculation = xlCalculationAutomatic

i = 0
End Sub

因此,我将Sheet(2)的起始编号(C列)复制到Sheet(1)的Cell(C5)中。当更改Sheet(1)中的单元格(C5)的值时,将重新计算Sheet(1).Range(C7:C16)中的值。我将新计算的值复制回Sheet(2),然后将其复制到正确的起始编号的行中。所以Sheet(1).Range(C7:C16)(两个值除外)在Sheet(2)中.Range(Di:Ki)(i,起始数的索引)。这个循环用于每个起始编号。

因此范围(C7:C16)保持结果。我的问题是,宏不等待计算完成。范围(Di:Ki)中的所有单元格都获得相同的值(在我启动宏之前,在(C7:C16)范围内的值)。

我用不同的方式尝试了它,如Application.CalculateFull所示

 Do Until Application.CalculationState = xlDone
        DoEvents
    Loop

还有一个Worksheet_Calculate()子。我也尝试过Worksheet_Change(),但它没有获取公式计算。

当我使用简单的公式,如基本添加等,它确实有效:例如。工作表(1)的公式.C7是C5 + 1(起始编号+ 1),我将获得工作表(2)

Starting Number - Result from Sheet(1).C7
1 - 2
2 - 3
3 - 4 

也许有人可以帮助我?提前致谢!

excel vba excel-vba copying
2个回答
0
投票

尝试使用这些花哨的技巧强制屏幕更新

  • ActiveSheet.Calculate
  • ActiveWindow.SmallScroll
  • Application.WindowState = Application.WindowState

借来自

Force a screen update in Excel VBA


0
投票

如果我理解你的问题,那么我相信以下会做你所期望的,它将从C列中的Sheets(2)获取值,并将该值传递给Sheets(1)单元格C5,这将使你的公式做到一些计算,然后它将采取C7:C16中的值,并将它们从列D到M放入表格(2),在同一行中它从初始值:

Private Sub CommandButton1_Click()
Dim i As Long
Application.Calculation = xlCalculationAutomatic
    For i = 2 To Sheets(2).Cells(Sheets(2).Rows.Count, "A").End(xlUp).Row
    'loop from row 2 to last on Sheets(2)
        Sheets(1).Cells(5, 3).Value = Sheets(2).Cells(i, 3).Value
        'add value from column C Sheets(2) to C5 of Sheets(1) for the formula calculations to take place
        Sheets(2).Cells(i, 4).Value = Sheets(1).Range("C7").Value
        'get the value from C7 and pass it to the relevant row in Sheets(2) on column D
        Sheets(2).Cells(i, 5).Value = Sheets(1).Range("C8").Value
        'get the value from C8 and pass it to the relevant row in Sheets(2) on column E
        Sheets(2).Cells(i, 6).Value = Sheets(1).Range("C9").Value
        'get the value from C9 and pass it to the relevant row in Sheets(2) on column F, and etc below
        Sheets(2).Cells(i, 7).Value = Sheets(1).Range("C10").Value
        Sheets(2).Cells(i, 8).Value = Sheets(1).Range("C11").Value
        Sheets(2).Cells(i, 9).Value = Sheets(1).Range("C12").Value
        Sheets(2).Cells(i, 10).Value = Sheets(1).Range("C13").Value
        Sheets(2).Cells(i, 11).Value = Sheets(1).Range("C14").Value
        Sheets(2).Cells(i, 12).Value = Sheets(1).Range("C15").Value
        Sheets(2).Cells(i, 13).Value = Sheets(1).Range("C16").Value
    Next i
End Sub

注意:

我删除了行Application.Calculation = xlCalculationManual,因为这会导致你的公式不会计算任何东西,直到你把它改回Application.Calculation = xlCalculationAutomatic。如果这不起作用,那么可能值得调查实际公式正在做什么,因为我已经测试了它并且它做了你期望的。

© www.soinside.com 2019 - 2024. All rights reserved.