我已经在Excel VBA中写了一些简单的代码,使一个范围“闪烁”的颜色-这是通过小心地绘制一个矩形对象在有问题的范围上,并改变其透明度,使框逐渐褪色。
以下是代码(在Worksheet_Change事件的Sheet1中):
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub flashBox(ByVal area As Range, Optional ByVal fillColour As MsoRGBType = vbGreen)
Const animationSeconds As Single = 0.5
Const animationSteps As Long = 20
With area.Worksheet.Shapes.AddShape(msoShapeRectangle, area.Left, area.Top, _
Application.WorksheetFunction.Min(area.Width, 1000), _
Application.WorksheetFunction.Min(area.Height, 1000)) 'stop infinite boxes, could use view area to define this
.Line.Visible = msoFalse
.Fill.ForeColor.RGB = fillColour
Dim i As Long
For i = 1 To animationSteps
Sleep animationSeconds * 1000 / animationSteps
.Fill.Transparency = i / animationSteps
DoEvents 'screen repaint
Next i
.Delete
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
flashBox Target, ColorConstants.vbMagenta
End Sub我使用With块来包含没有父变量的临时对象(我认为它非常整洁,并希望保持这种方式)。该问题源于DoEvents调用(强制为动画重绘屏幕所必需的)。
动画是从特定的工作表更改事件中调用的,如果调用子对象的第二个实例(或者实际上,如果发生了任何事件),第一个实例将终止一半,并且永远不会完成-这意味着临时形状永远不会被删除。
下面的插图展示了我正在谈论的内容:

我该如何解决这个问题呢?
发布于 2019-05-12 04:24:09
与其说它是一种解决方案,不如说它是一种锻炼,但从技术上讲,它可以完成工作
如果您在flashbox过程的运行时禁用用户输入,它将等待动画进入芬兰,然后再重新启用它,您将避免动画处于冻结状态
Private Sub Worksheet_Change(ByVal Target As Range)
Application.Interactive = False
flashBox Target, ColorConstants.vbMagenta
Application.Interactive = True
End Sub

我会看看我是否能“恰当地”解决这个问题,但现在这是一个很好的变通方法:)
https://stackoverflow.com/questions/56093001
复制相似问题