14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

498 Extending JMP Chapter 14<br />

OLE Automation<br />

Begin by opening Microsoft Excel. To create a Visual Basic script for an Excel workbook, select<br />

Visual Basic from the Developer ribbon. The Visual Basic editor opens in a separate window. On the left<br />

side of the Visual basic editor, there is a pane entitled VBA Project. This pane shows the sheets that might<br />

have Visual Basic code associated with them, as well as the workbook itself.<br />

Figure 14.5 VBA Project for Excel<br />

Code written for the workbook usually works for any of the sheets within the workbook.<br />

There are three sections involved in the coding for this example. First, there are some variables that are<br />

global in scope that are declared in the module1.bas file. This allows these variables to be referenced in<br />

other code modules. A module can be inserted into the Visual Basic project by context-clicking on the VBA<br />

project icon and selecting Insert > Module. Type the following code into the module. The code declares<br />

instances of a JMP application, a JMP data table, and a flag to keep track of whether a document is open or<br />

not.<br />

Public MyJMP as JMP.Application ‘The JMP Application Object<br />

Public DT As JMP.DataTable ‘The JMP Data Table object<br />

Public DocOpen as Boolean ‘A flag indicating “JMP Table Open”<br />

The next segment updates JMP when cells in the Excel worksheet change. It is called automatically because<br />

Excel generates the Worksheet_change event whenever a cell is changed, deleted, or added.<br />

The Excel VBA Project Browser shows the sheets that are currently part of the workbook. The code below<br />

should be placed in the sheet that sends data to JMP. Double-click on the sheet icon in the VBA Project<br />

Window to bring up the code for that particular sheet.<br />

Private Sub Worksheet_change(ByVal Target as Range)<br />

Dim Col as JMP.Column<br />

If(DocOpen) Then<br />

If(Target.Row = 1) Then<br />

Return<br />

End If<br />

If(DT.NumberRows < Target.Row - 1) Then<br />

DT.AddRows Target.Row - DT.NumberRows - 1, Target.Row<br />

End If<br />

If(Not IsArray(Target.Value) And Not IsEmpty(Target.Value)) Then<br />

Set Col = DT.GetColumnByIndex(Target.Column)<br />

Col.SetCellVal Target.Row - 1, Target.Value<br />

End If

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!