首页 > 解决方案 > 移动带有插入行的形状,其中 Application.ScreenUpdating = False

问题描述

在一个大型项目中,我在工作表的不同位置添加了大量形状。此外,我插入了许多行。

我希望形状与插入的行一起移动。但是,他们只使用Application.ScreenUpdating = True. 一旦设置 ScreenUpdating False,形状就会停止移动。这当然会完全弄乱结果。

我无法重现该问题。在这个最小的示例中,插入的形状会随着插入的行按预期移动,尽管我使用Application.ScreenUpdating = False. 在我较大的程序中,基本相同的程序在没有 ScreenUdating 的情况下失败。

Sub ShapeTest()

Dim ActiveShape As Shape
Dim ShapeCell As Range

Application.ScreenUpdating = False

Set ShapeCell = ActiveSheet.Range("A1")
Set ActiveShape = ActiveSheet.Shapes.AddShape(msoShapeRectangle, ShapeCell.Left, ShapeCell.Top, ShapeCell.Width, ShapeCell.Height)

ActiveSheet.Rows(1).Insert shift:=xlShiftDown

Application.ScreenUpdating = True

End Sub

更新

DoEvents在插入行之前和之后都尝试过,但它没有改变任何东西。目前我正在使用这种解决方法:

Application.ScreenUpdating = True
Worksheets("Gantt").Rows(ThisRowGTT).Insert shift:=xlShiftDown
Application.ScreenUpdating = False

这会减慢执行速度——几乎就像我会为整个程序使用 ScreenUpdating 一样。

标签: excelvbainsertshapes

解决方案


推荐阅读