## VideoPhoto

Showing posts with label VBA code. Show all posts
Showing posts with label VBA code. Show all posts

## 27 July, 2023

### Shapes Based On Parametric Equations: Hypotrochoids

Hypotrochoids are curves generated by a point selected on a circle of smaller radius rolling around the inside of a fixed circle of larger radius, and that selected point can be chosen at some distance from the center of the smaller circle. These are then 3-parametric curves.

You don't need any special programs, like e.g. Mathematica, to create hypotrochoids. They can be generated in Excel quite easily by using VBA macro like the one provided at the end of this post.

Here are some examples of charts with generated hypocycloid curves, both single and combined double curves:

### Shapes Based On Parametric Equations: Hypocycloids

Hypocycloid is a parametric curve traced by a fixed point on a circle that rolls on the interior of another larger circle.

The curve - due to its parametric functionality - finds many applications, both theoretical and practical. They include mechanical and construction engineering, e.g. design of gears, cams, valves, robotics (motion planning), and machine/structures design in general. Contribute also to aesthetically pleasing art designs and animations.

The hypocycloid curves can be generated quite easily in Excel with VBA macro,  presented at the end of this post. Here are some examples of charts with generated hypocycloid curves, both single and combined double curves:

## 26 May, 2023

### Creating Shapes, Graphics, Illustrations - Part 2

Here is yet another way of creating graphics in Excel - using VBA macro, like this one:

Sub DrawDefShape()
'Draws any defined shape representing Bézier curves
Dim x As Integer, y As Integer
Dim pt(1 To 7, 1 To 2) As Single
Dim rng As Range
Set rng = Sheets("Data").Range("A2:B8")
'Fill array of control point coordinates with values
For x = 1 To 7
For y = 1 To 2
pt(x, y) = rng.Cells(x, y).Value
Next y
Next x
Set myDoc = Worksheets("Data")
myDoc.Shapes.AddCurve SafeArrayOfPoints:=pt
Range("A10").Select
'Display the whole 2D array in MsgBox
For i = 1 To UBound(pt)
dPts = dPts & pt(i, 1) & "   " & pt(i, 2) & vbNewLine
Next i
MsgBox dPts
End Sub

Before going into the details of the macro structure and its use, let's look at some examples of curves and shapes you can easily create with it. Here they are:

## 30 April, 2023

### Jumping Across Cells with Enter Key

Normally, in Excel, when you press Enter key - depending on your initial selection of editing options - you move in one of the directions (Down, Right, Up, Left), as shown in the snippet, by just one cell. There are no other options available in Excel settings. I'm using the Down option for the purpose of this post.

So, what you can do, if for some reason you definitely need to move automatically and repeatedly from the current cell by more than one cell and in any selected direction?

## 20 January, 2022

### Animation - Flying Objects

The following VBA (Visual Basic Application) code makes Excel shapes and some other graphics flying. In this instance I'm shooting a rocket on my worksheet across the computer screen. This shape is based on Excel graphic named "Graphic 4".

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal iMilliseconds As Long)

Sub Flyer()
Sheet1.Shapes("Graphic 4").Left = 1000   'Starting X
Sheet1.Shapes("Graphic 4").Top = 450   'Starting Y
MoveShp Sheet1.Shapes("Graphic 4"), 0!, 0!, #12:00:01 AM#   'Shape inserted
End Sub
Sub MoveShp(shp As Shape, ByVal coLeft As Single, ByVal coTop As Single, t As Date)
' Moves the shape from start to finish over the interval t
Const xch = 0.018
Const n1 As Long = 30       'Accelerate/decelerate steps
Const n2 As Long = 60   'Coast steps
Const n As Long = 2 * n1 + n2    'Total steps
Dim i As Long   'Step index
Dim stpv As Single   'Coasting, pixels/step
Dim v As Single     'Velocity at current step
Dim cLiLeft As Single, cLiTop As Single   'Left and Top num
Dim cMi As Single      'Frctn denom
Dim coLeftPr As Single, coTopPr As Single  'Previous Left and Top position
stpv = 1 / (n - n1)
With shp
coLeft = coLeft - .Left: coTop = coTop - .Top
coLeftPr = .Left: coTopPr = .Top
For i = 1 To n
Select Case i
Case 1 To n1    'Accelerate
v = stpv * (1 + Cos(xch * 180 * (1 + i / n1))) / 2
Case n1 + 1 To n - n1   'Constant velocity
v = stpv
Case Else       'Decelerate
v = stpv * (1 + Cos(xch * 180 * (1 + (n - i) / n1))) / 2
End Select
.Left = .Left + v * (coLeft - cLiLeft) / (1 - cMi)
.Top = .Top + v * (coTop - cLiTop) / (1 - cMi)
cMi = cMi + v
cLiLeft = .Left - coLeftPr: cLiTop = .Top - coTopPr
DoEvents
Sleep t * 86400000# / n
Next i
End With
End Sub

To use the code in Excel on your computer follow these steps:

• find and insert Graphic 4 shape into your worksheet (if you want to use any other Excel graphic, remember to change the shape name in the above code)
• copy the code and paste it into any module of your project (workbook)
• select the "Flyer" macro from Macros in the ribbon to run it.

If you assign the macro to the shape (by right-clicking it and selecting "Assign Macro..." option), then just click on the shape and it will fly...

Try to run it using some other shapes/objects.

## 16 December, 2021

### How to create Activity Tracker / Timer

If you'd like to keep in Excel the record of your common daily tasks, in terms of disciplined use of your valuable time, then the solution I'm providing here can be helpful. It makes easy recording of duration of any routine daily activities and provides basis for further analysis of any kind.

This is how it looks like in an exemplary edition:

## 23 May, 2021

### Pick your lucky Lotto numbers

If you play Lotto, how do you pick your lucky numbers?

Everyone has their own method. Some people like to analyze past draws and study the statistics to select potential winning patterns. Others rely on totally random ways. My own experience shows that statistics helps a little bit with winnings at the low end only. That’s it.

No method can guarantee a big win. Lotteries are completely random. To check out my 'independent' serendipity - years ago - I decided to create my own Lotto number picker coded with VBA. It’s one of my modest and oldest Excel creations. Here's its face in a worksheet:

Simple and functional. It just picks six lucky numbers for you to play in a lottery like Lotto649 or something close to it. It can be modified quite easily for other lottery games based on random selections. You can get your very own quick picks on your own computer.