Previous Topic

Next Topic

Book Contents

Book Index

MS Excel VBA Sample Code

A sample spreadsheet tool is included with the installation. We encourage users to reference the VBA code in that spreadsheet.

There are two ways to use the COM interface from Microsoft Excel VBA or any other VBA:

The first technique is to use Tools ... References in the VBA Editor to add a reference to the Random Numbers.tlb file located in the program folder. This will cause the class "RandomNumbers.Generator" to become a class available in the Object Browser and through AutoComplete in the VBA Editor.

The second technique is more generic. It involves declaring an object variable and using CreateObject("RandomNumbers.Generator") to create the Generator object prior to calling methods.

The sample Excel VBA code below illustrates the second technique:

Public Sub Test_Click()

' Doesn't use the type library.

Dim objGenerator As Object

Dim vResult As Variant

' Create the object.

Set objGenerator = CreateObject("RandomNumbers.Generator")

Sheets("Sheet1").Activate

' Retrieve buffer data

Range("E6").Value = objGenerator.BufferedData

Range("E7").Value = objGenerator.BufferStatus

' Retrieve Random numbers

Range("E11").Value = objGenerator.RandomInteger(100)

Range("E12").Value = objGenerator.RandomDouble

Range("E14").Activate

' Retrieve Binary Data.

vResult = objGenerator.RandomData(1000)

' Test result.

If IsEmpty(vResult) = False Then

' Loop through data and display

For Counter = 0 To UBound(vResult)

ActiveCell.Value = vResult(Counter)

' Next row.

ActiveCell.Offset(1, 0).Activate

Next

Else

ActiveCell.Value = "Error"

End If

' Jump back to the top.

Range("A1").Activate

End Sub

See Also

Using the COM Interface

MS Visual Basic Sample Code

MS Visual C++ Sample Code

VBScript Web Password Sample Code