An Erroneous Example

To get things started, I developed a short VBA macro. Activate the VBE, insert a module, and enter the following code:

Sub EnterSquareRoot()

Dim Num As Double

' Prompt for a value

Num = InputBox("Enter a

value")

' Insert the square root

ActiveCell.Value = Sqr(f

^um)

End Sub

As shown in Figure 12-1, this procedure asks the user for a value. It then enters the square root of that value into the active cell.

Figure 12-1:

The InputBox function displays a dialog box that asks the user for a value.

Kit I. Has

A

S' I

M D E | F I Mi I H

-1

1

2

«

H

Bïtërdvalue jK |

A

6

pjhcgl I

■m.

8

9

I

m

11

V

ft :r

f Sheet l j

f<l

>

You can execute this procedure directly from the VBE by pressing F5. Alternatively, you may want to add a button to a worksheet (use the Forms toolbar to do this) and then assign the macro to the button. (Excel prompts you for the macro to assign.) Then you can run the procedure by simply clicking the button.

0 0

Post a comment