在之前的文章中,我们使用XY散点图绘制了圆形运动轨迹动画。由于使用的数据是基于工作表中的25个数据,数据量太少,所以运动轨迹看起来并不圆。
本文也是制作圆形运动轨迹动画,但不使用工作表中的数据,而是使用由定义的名称所生成的数据。
为了方便绘制,我们使用实用中已经使用的绘图区作为模板。
首先,定义一些名称。
名称:n
引用位置:=200
名称:ndx
引用位置:=1
名称:t
引用位置:=(ROW(OFFSET(Sheet1!$A$1,,,n))-1)*(2*PI()/(n-1))
名称:circle_x
引用位置:=COS((ROW(OFFSET(Sheet1!$A$1,,,50))-1)*(2*PI()/(50-1)))
名称:circle_y
引用位置:=SIN((ROW(OFFSET(Sheet1!$A$1,,,50))-1)*(2*PI()/(50-1)))
名称:circle_path_x
引用位置:=COS(t)
名称:circle_path_y
引用位置:==SIN(t)
名称:dot_circle_x
引用位置:=INDEX(circle_path_x,ndx)
名称:dot_circle_y
引用位置:=INDEX(circle_path_y,ndx)
选择绘图区,在“图表工具”选项卡组中选择“设计”选项卡下的“选择数据”命令,添加数据系列dot_circle,如图1所示

图4
在工作表中插入一个ActiveX控件命令按钮,将其标题更改为“圆形轨迹运动”,双击该按钮并输入下面的代码:
Private i As Long
PrivateblnRunning As Boolean
Private SubCommandButton1_Click()
Dim nm As Names
Dim z As Long
Const DELAY_FACTOR = 125
If CommandButton1.Caption = “圆形轨迹运动” Then
CommandButton1.Caption = “停止运动”
End If
If blnRunning Then
blnRunning = False
CommandButton1.Caption = “圆形轨迹运动”
Exit Sub
End If
blnRunning = True
Application.Cursor = xlNorthwestArrow
Set nm = Application.Names
Do
If blnRunning = False Then Exit Do
i = i + 1
If i > 800 Then i = 1
nm.Add “ndx”, (i Mod [n]) + 1
For z = 1 To DELAY_FACTOR: DoEvents:Next z
Loop






