Example A Code to Produce a Random Series of Stars

Sub DrawName()

' Random placement of large stars with name

Const pi = 3.1416

Dim

i As Integer

Dim

x As Single, y As

Single

Dim

z As Single

Dim

rng As Range '

For starting point

Dim

n As Single '

Cycle length in inches

Dim

k As Integer '

k stars

Dim

sSize As Single '

Star size

Dim

sh As Shape

Dim

sName As String

Name to display

Dim

StartLeft As Integer

Dim

StartTop As Integer

Tea417fy®

' Starting position StartLeft = ActiveCell.Left StartTop = ActiveCell.Top sName = "Steven Roman" n = 5

sSize = Application.InchesToPoints(0.5)

Randomize Timer z = 0#

' Loop for first curve with phase shift For i = 1 To k

If Mid(sName, i, 1) <> " " Then x = n * i / k x = Application.InchesToPoints(x)

' Get random 0 or 1. Go up or down accordingly. If Int(2 * Rnd) = 0 Then z = z + 0.2 Else z = z - 0.2 End If y = Application.InchesToPoints(z) Set sh = ActiveSheet.Shapes.AddShape

(msoShape5pointStar, StartLeft + x, StartTop + y, sSize, sSize)

' Add shading sh.Fill.ForeColor.RGB = RGB(230, 230, 230) sh.Fill.Visible = msoTrue

' Add text sh.TextFrame.Characters.Text = Mid(sName, i, 1) sh.TextFrame.Characters.Font.Size = 10 sh.TextFrame.Characters.Font.Name = "Arial" sh.TextFrame.Characters.Font.Bold = True End If Next i End Sub

The output from this code is shown in Figure A-4.

0 0

Post a comment