Dynamic Macros 1
Dynamic Macros 1
Scientific Modeling
part #1
by George Lungu
<excelunusual.com>
Excel is generally used as a static tool. As an Excel companion, Visual Basic for
Applications (VBA) is an event-driven programming language which allows
processes automation and the creation of user defined functions. My first
experience with VBA was in creation of simple control interfaces (e.g. buttons) or
running through simple calculation loops in order to achieve a certain result.
Beginners can use a static time dimension which means some parameters of a
model are calculated in cells situated along a horizontal line. The time however will
advance vertically. In this case we obtain a fixed size table of values in which time is
located on one of the columns. The information can be presented on a scatter plot. If
we can figure out a way to chart one subset of this table and vary the position of the
subset in real time we can animate our model which means we can give the illusion
of movement on a chart.
How can we do this?
By using the spreadsheet function Offset() and varying the argument controlling the
offset value through a VBA macro.
<excelunusual.com>
<excelunusual.com>
<excelunusual.com>
<excelunusual.com>
Use the OFFSET() function to isolate a certain row in the output table:
Create the following labels:
- Cell B7: Index , Cell E10: Selection
Choose an arbitrary index number:
Cell C7: =5
Use the OFFSET() function:
- Cell E11: OFFSET(C11,C7,0)
(note here that by changing the index
(cell C7), we can select a different cell
from column C to be displayed in cell
E11
Create a Column Chart out of cell E11:
- Select cell E11 => Insert => Chart =>
Column => Finish
- Format the vertical axis between -1
and 1
<excelunusual.com>
<excelunusual.com>
Dim n As Integer
Sub Loop1()
n=0
For n = 0 To 1000
DoEvents
Range("C7") = n
Next
End Sub
The DoEvents function surrenders execution of the macro so that the operating
system can process other events.
If the DoEvents statement were missing the macro would work fine but the chart
would not update properly or we wouldnt be able to run other macros such as
changing parameters on the spreadsheet during the time the loop is active.
<excelunusual.com>
Sub Loop2()
n=0
Do
DoEvents
Range("C7") = n
n=n+1
Loop Until n > 1000
End Sub
The DoEvents function surrenders execution of the macro so that the operating
system can process other events.
If the DoEvents statement were missing the macro would work all right but the
chart would not update properly or we wouldnt be able to run other macros such as
changing parameters on the spreadsheet
<excelunusual.com>
In the case of the third loop, we will use a Do loop with an incorporated beginning
condition (n<1001) for running it (once this condition is violated the loop stops):
Sub Loop3()
n=0
Do While n < 1001
DoEvents
Range("C7") = n
n=n+1
Loop
End Sub
The DoEvents function surrenders execution of the macro so that the operating
system can process other events.
If the DoEvents statement were missing the macro would work all right but the
chart would not update properly or we wouldnt be able to run other macros such as
changing parameters on the spreadsheet
<excelunusual.com>
10
<excelunusual.com>
11
Observations:
1. A macro can be assigned to almost any object in the spreadsheet : a shape, a piece
of text, a graph or any type of picture.
2. In order to give a limited amount of protection to a chart we can assign the chart to
the following macro:
The way this macro operates is by selecting a
spreadsheet cell when the chart is highlighted.
Sub ChartProtect()
The macro will deflect any attempt to change the
Range("E15").Select
graphed data until the macro is de-assigned
End Sub
from the chart.
Right click the first chart in the work sheet and assign the macro ChartProtect(),
and then try to highlight the sinusoidal curve. What happens? It wont work. You
got some weak chart curve protection (against mistakes). You can still move the
chart or even resize it by dragging one of its corners.
You can always un-assign any macro from a shape, button, chart or picture
by right clicking it and deleting the macro in the assign window.
<excelunusual.com>
12
Sub Loop4()
n=0
Do
DoEvents
If n > 1000 Then
Exit Do
Else
Range("C7") = n
n=n+1
End If
Loop
End Sub
<excelunusual.com>
Loop 4
13
<excelunusual.com>
14
The Syntax:
Dim n As Integer
Dim RunSim As Boolean
--------------------------------------Sub StartStop()
RunSim = Not (RunSim)
Do While RunSim = True
DoEvents
Range("C7") = n
n=n+1
Loop
End Sub
--------------------------------------Sub reset()
RunSim = False
n=0
Range("C7") = 0
End Sub
<excelunusual.com>
15
Implementation:
- Two buttons were created from rounded
rectangle drawing shapes, then the
Run/Pause and Reset macros were
assigned to them.
- The macros code is placed in the
Module2 section .
- The reader must check the functionality.
Conclusions:
- Dynamic macros were created using two different type of loops: For and Do.
- Two different but equivalent conditional Do loops were explored.
- It was shown that macros can be assigned to drawing shapes, pictures, graph, etc.
- A very valuable Run/Pause macro was described and implemented
The reader must pay close attention to this presentation. In order to create science
or engineering models one needs these techniques.
<excelunusual.com>
16