Hvis du vil have det med analog visning kan du bruge nedenstående (fundet på
http://www.cpearson.com/excel/download.aspx og lettere modificeret):
Option Explicit
'''
Const cCenterX As Single = 250
Const cCenterY As Single = cCenterX
Const cLenSecond As Single = cCenterX * 0.6
Const cLenMinute As Single = cLenSecond * 0.85
Const cLenHour As Single = cLenSecond * 0.6
Const cFaceRadius As Single = cLenMinute * 1.25
'''
Const PI As Single = 3.14159265358979
Const TwoPI As Single = 2 * PI
Dim RunWhen As Double
Dim LS As Shape
Dim LM As Shape
Dim LH As Shape
Dim LLS As LineFormat
Dim LLM As LineFormat
Dim LLH As LineFormat
Dim Face As Shape
Dim FaceNumbers(1 To 12) As Shape
Dim ChipLabel As Shape
Dim CenterPiece As Shape
Private Sub Init()
On Error Resume Next
Dim WS As Worksheet
Dim Ndx As Integer
Dim L As Single
Dim T As Single
Dim H As Single
Dim W As Single
Dim Theta As Single ' clockwise angle from vertical
Dim NewX As Single
Dim NewY As Single
Set WS = ActiveSheet
' delete the existing shapes
With WS.Shapes
.Item("LineSecond").Delete
.Item("LineMinute").Delete
.Item("LineHour").Delete
.Item("ClockFace").Delete
.Item("CenterPiece").Delete
For Ndx = 1 To 12
.Item("Number" & Ndx).Delete
Next Ndx
.Item("ChipLabel").Delete
End With
On Error GoTo 0
' create the numbers
For Ndx = 1 To 12
Theta = (Ndx / 12) * TwoPI
W = 20
H = 20
L = cCenterX + (cFaceRadius * Sin(Theta)) - (W / 2)
T = cCenterY - (cFaceRadius * Cos(Theta)) - (H / 2)
Set FaceNumbers(Ndx) = WS.Shapes.AddLabel(msoTextOrientationHorizontal, L, T, W, H)
With FaceNumbers(Ndx)
.Name = "Number" & Ndx
.TextFrame.Characters(1, 1).Caption = Format(Ndx)
.TextFrame.Characters(1, 2).Font.Bold = True
.TextFrame.Characters(1, 2).Font.Size = 14
End With
Next Ndx
'create the face
Set Face = WS.Shapes.AddShape(Type:=msoShapeOval, Left:=FaceNumbers(9).Left, Top:=FaceNumbers(12).Top, _
Width:=FaceNumbers(3).Left + FaceNumbers(3).Width - FaceNumbers(9).Left, _
Height:=FaceNumbers(6).Top - FaceNumbers(12).Top + FaceNumbers(6).Height)
With Face
.Fill.BackColor.RGB = RGB(255, 255, 255)
.Name = "ClockFace"
.ZOrder msoSendToBack
End With
Set ChipLabel = WS.Shapes.AddLabel(msoTextOrientationHorizontal, cCenterX, _
(cCenterY + cFaceRadius / 2), Width:=100, Height:=20)
With ChipLabel
With .TextFrame
.Characters(1, 100).Caption = "Du har tiden, vi har uret"
.Characters(1, 100).Font.Bold = True
.Characters(1, 100).Font.Color = RGB(255, 0, 0)
End With
.IncrementLeft ChipLabel.Width * -0.5
.Name = "ChipLabel"
End With
Set CenterPiece = WS.Shapes.AddShape(msoShapeOval, Left:=cCenterX - 10, _
Top:=cCenterY - 10, Width:=20, Height:=20)
With CenterPiece
.Fill.ForeColor.RGB = RGB(190, 0, 255)
.Name = "CenterPiece"
End With
' create second hand
Set LS = WS.Shapes.AddLine(beginx:=cCenterX, beginy:=cCenterY, _
endx:=cCenterX, endy:=cCenterY - cLenSecond)
Set LLS = LS.Line
LS.Name = "LineSecond"
LLS.EndArrowheadStyle = msoArrowheadOpen
LLS.ForeColor.RGB = RGB(255, 0, 0)
LLS.Weight = 1
' create minute hand
Set LM = WS.Shapes.AddLine(beginx:=cCenterX, beginy:=cCenterY, _
endx:=cCenterX, endy:=cCenterY - cLenMinute)
Set LLM = LM.Line
LM.Name = "LineMinute"
LLM.EndArrowheadStyle = msoArrowheadTriangle
LLM.ForeColor.RGB = RGB(0, 0, 255)
LLM.Weight = 1.5
' create hour hand
Set LH = WS.Shapes.AddLine(beginx:=cCenterX, beginy:=cCenterY, _
endx:=cCenterX, endy:=cCenterY - cLenHour)
Set LLH = LH.Line
LH.Name = "LineHour"
LLH.EndArrowheadStyle = msoArrowheadTriangle
LLH.ForeColor.RGB = RGB(33, 100, 66)
LLH.Weight = 1.75
' move things to back and forth as needed
'
CenterPiece.ZOrder msoBringToFront
End Sub
Private Sub SetSecondHand(Seconds As Single)
Dim Theta As Single ' clockwise angle from vertical
Dim NewX As Single
Dim NewY As Single
Theta = (Seconds / 60) * TwoPI
NewX = cCenterX + (cLenSecond * Sin(Theta))
NewY = cCenterY - (cLenSecond * Cos(Theta))
LS.Nodes.SetPosition 2, NewX, NewY
End Sub
Sub SetMinuteHand(Minutes As Single)
Dim Theta As Single ' clockwise angle from vertical
Dim NewX As Single
Dim NewY As Single
Theta = (Minutes / 60) * TwoPI
NewX = cCenterX + (cLenMinute * Sin(Theta))
NewY = cCenterY - (cLenMinute * Cos(Theta))
LM.Nodes.SetPosition 2, NewX, NewY
End Sub
Private Sub SetHourHand(Hour As Single)
Dim Theta As Single ' clockwise angle from vertical
Dim NewX As Single
Dim NewY As Single
Theta = (Hour / CSng(12)) * TwoPI
NewX = cCenterX + (cLenHour * Sin(Theta))
NewY = cCenterY - (cLenHour * Cos(Theta))
LH.Nodes.SetPosition 2, NewX, NewY
End Sub
Public Sub RunClock()
If RunWhen = 0 Then
Init
End If
SetSecondHand Second(Now)
SetMinuteHand Minute(Now)
SetHourHand Hour(Now) + (Minute(Now) / 60)
RunWhen = Now + TimeSerial(0, 0, 1)
Application.OnTime RunWhen, "RunClock", , True
End Sub
Public Sub StopClock()
On Error Resume Next
Application.OnTime RunWhen, "RunClock", , False
RunWhen = 0
End Sub
Sub Auto_Close()
StopClock
End Sub