Finance

Charts

Statistics

Macros

Search

Slide show with Excel VBA

The Image control allows you to create a simple slide viewer.

As a demonstration, let us create an application for viewing several images.

Create a form with an Image control and a list. In addition, you will need files with corresponding raster images.

In the form module, enter the required code. That’s all — the project is ready.

Modified Chart Wizard
Building a chart with the Chart Wizard normally requires a large amount of preparatory work:

  • filling one range with argument values,
  • filling another range with function values,
  • and then building the chart through a four-step wizard.

Let us create an application that will fill the ranges with argument and function values, build the chart, and display it not only on the worksheet but also in a form.

In this application, the user will only need to enter the interval boundaries and the function itself. The function should be entered not as a worksheet formula but in the usual form, using the symbol « z » as its argument.

The use of « z » as the argument is intended to simplify the code.

Constructing the application
Create a form with three labels, three text boxes, an image, and a button.
Set their Name properties in the Properties window as shown in Table.

Table. Property values of controls set in the Properties window

Control Name Description
TextBox txtBegin Input of the left boundary of the interval
Label lblBegin Label corresponding to txtBegin
TextBox txtEnd Input of the right boundary of the interval
Label lblEnd Label corresponding to txtEnd
TextBox txtFun Input of the function formula. The formula must follow programming rules, with « z » as the argument.
Label lblFun Label corresponding to txtFun
Image imgFun Displays a raster image from Graph.gif, which contains the graphic representation of the chart.
Button cmdReady Triggers the building of the chart.

How the application works
The chart is built from 101 points. First, the step size of the argument is calculated as the difference between the start and end values divided by 100.

The initial argument value is placed in cell A2.
Using the DataSeries method, the argument values are tabulated down the column.

In the function formula, the argument « z » is replaced with a reference to cell A2, and the formula is prefixed with the equals sign « = ».

This formula is entered into cell B2.
With the AutoFill method, the formula is filled down the column to compute the function values for all tabulated arguments.

Based on the argument and function values, a chart is built, and its graphic representation is exported to the file Graph.gif.

The raster image from Graph.gif is then displayed in the Image control imgFun.

Why “z” is used as the argument
Using « z » as the parameter makes the code simpler, since « z » is not part of any built-in worksheet function names.

Thus, it is sufficient to use the single instruction:

f = Replace(LCase(f), « z », « A2 »)

This replaces all occurrences of « z » with the reference to cell A2.

If « x » were used instead, one instruction would not be enough, because « x » appears in built-in function names (for example, Exp()).
In such a case, the instruction would replace not only the argument but also parts of function names, turning Exp() into EA2p(), which would result in an error.

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx