Monday, June 2, 2008

Spinning Excel Pie Chart

Have you ever wanted to create a pie chart in Excel that spins? I didn't think so, but anyway, below is a little bit of VBA that will do the job if you've got a toggle button on your spreadsheet (ToggleButton1).



Option Explicit

Dim Spinning As Boolean
Dim RotateCalled As Integer

Public Sub Rotate()
'http://msdn.microsoft.com/en-us/library/aa173240(office.11).aspx
    RotateCalled = RotateCalled + 1
    If RotateCalled > 1 Then Exit Sub   ' Only call once
    
    Dim x As Integer
    
    Do
        If Spinning = True Then
            Worksheets("Sheet1").ChartObjects(1).Chart.PieGroups(1).FirstSliceAngle = x
            x = x + 1
            If x = 360 Then x = 0
        End If
        Call Wait(0.05)
    Loop
End Sub

Sub Wait(tSecs As Single)
    Dim sngSec As Single
     
    sngSec = Timer + tSecs
    Do While Timer < sngSec
        DoEvents
    Loop
End Sub

Private Sub ToggleButton1_Click()
    If ToggleButton1.Value = True Then
      Spinning = True
      ToggleButton1.Caption = "Stop"
    Else
      Spinning = False
      ToggleButton1.Caption = "Go"
    End If
    
    Call Rotate
End Sub
 1 Then Exit Sub   ' Only call once
    
    Dim x As Integer
    
    Do
        If Spinning = True Then
            Worksheets("Sheet1").ChartObjects(1).Chart.PieGroups(1).FirstSliceAngle = x
            x = x + 1
            If x = 360 Then x = 0
        End If
        Call Wait(0.05)
    Loop
End Sub

Sub Wait(tSecs As Single)
    Dim sngSec As Single
     
    sngSec = Timer + tSecs
    Do While Timer < sngSec
        DoEvents
    Loop
End Sub

Private Sub ToggleButton1_Click()
    If ToggleButton1.Value = True Then
      Spinning = True
      ToggleButton1.Caption = "Stop"
    Else
      Spinning = False
      ToggleButton1.Caption = "Go"
    End If
    
    Call Rotate
End Sub



Wayne's World of IT (WWoIT), Copyright 2008 Wayne Martin.

No comments:

Post a Comment