如何将表格渲染推迟到操作结束?

时间:2014-07-15 10:02:30

标签: excel excel-vba ms-office vba

我使用Excel以图形方式检查嵌入式遥测数据。为此,我使用嵌入式软件将内存数组以十六进制格式转储到文本文件中。然后我使用Excel的“文本导入向导”导入文件的内容,使用剪切和粘贴。

在Excel中,我使用VBA宏将十六进制字符串转换为十进制值,然后再进行操作和绘图。对于每个源hex单元格,我有一个“图像”单元格,其中包含hex-to-dec翻译公式。

数组由数百个到数千个值组成。因此,当在“输入”区域上导入/粘贴十六进制数据时,使用宏自动计算“输出”区域。问题是Excel在每次计算后刷新表(显然)。我发现this question在执行宏期间解析了更新。但是,我的工作表会进行数百次简单和简短计算,因此无效。

如何在“粘贴”操作结束前阻止更新工作表?请注意,我可以禁用自动更新。但是,我确实希望启用此选项,但特定的文本导入操作除外。


更新:这是我使用的宏:

Type MyHex
Lng As Long
End Type
Type MySingle
sng As Single
End Type

Function Hex2Ieee754(i As Long)
Dim h As MyHex
Dim s As MySingle
h.Lng = i

LSet s = h
Hex2Ieee754 = s.sng

End Function

1 个答案:

答案 0 :(得分:2)

将您的整个代码包含在

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False

' .... your code goes here


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True

在执行此操作之前,请彻底测试您的代码,因为如果执行需要很长时间,则禁用事件会使您的应用程序无法使用。

<强>更新

代码的瓶颈是更新单元格的方式,而不是内部计算本身。

可以使用以下步骤
  1. 通过一次操作从电子表格中获取所有值
  2. 通过一次操作将所有计算值推送到电子表格
  3. 代码:

    Dim InputRng as Range
    Set InputRng = ActiveSheet.Range("A1:A10")
    
    Dim InputVar() as Variant
    InputVar = InputRng
    ' All data fetched in a single operation
    
    'Now InputVar(1,1) contains the top left element of the range i.e. content of A1 cell
    
    'You may use code such as
    
    Dim LngVariable as Long
    LngVariable = CLng(InputVar(1,1) + 23.232)
    ' etc.....
    
    Dim OutputVar() as Variant
    ReDim OutputVar(1 to 10, 1 to 1)    ' You need to output a range of 10 rows and 1 column
    'Set elements of OutputVar to what you would like your Output range to be
    ' OutputVar(1,1) will be the top left element, etc.
    
    Dim OutputRng as Range
    Set OutputRng = ActiveSheet.Range("B1:B10")
    
    OutputRng = OutputVar
    ' All data pushed onto sheet in a single operation
    

    此代码将比原始代码快几个数量级。您可以修改它以适合您的程序逻辑。