How can I attach a macro program to a toolbar button (keyboard command, or menu option)?

One common application of Statistica Visual Basic macro programs is to "enhance" the Statistica functionality by creating toolbar buttons or keyboard commands (e.g., CTRL+key sequences) that allow you to quickly perform a series of operations or routine analyses. Shown below is a simple program that will sort a spreadsheet in a workbook, by the (first) selected column.

' This program will sort the spreadsheet data by ' the currently selected column (the first column ' in a range of selected columns). The program ' assumes that the spreadsheet is inside the ' currently active workbook. Sub Main

' The following line will cause the program to ' resume at label ErrOut, if any error occurs (e.g., ' if a workbook or spreadsheet can't be found). On Error GoTo ErrOut Dim ss As Spreadsheet Dim wb As Workbook Dim r As Range Dim ifault As Integer ifault=1 Set wb=ActiveWorkbook Set ss=wb.ActiveItem.Object ifault=2 Set r=ss.ActiveCell ifault=0 ss.SortData(r.Column) Exit Sub ErrOut: If ifault=1 Then

MsgBox "Nothing to sort; the program assumes that you" & _ " have a Spreadsheet with numbers highlighted in a Workbook."

ElseIf ifault=2 Then

MsgBox "No column is currently highlighted; nothing to sort."

Else

MsgBox "Cannot sort data."

End If

End Sub

First, create this macro by selecting File - New, and then on the Macro tab call this new macro program SortColumn; then click the OK button. Next type (or paste) in this program, and save it (e.g., as SortColumn.svb).

Select Customize from the Tools menu, and select Macros in the Categories list.

Now drag the SortColumn macro in the Command list onto the toolbar. A new toolbar button will be created, identified by the name of the macro.

In the illustration above, a "smiley face" was added to the SortColumn button. To do this, select Tools - Customize; then, while the Customize dialog remains displayed, right-click on the SortColumn button and select Button Appearance.

The Button Appearance dialog is displayed, where you can select a predefined icon or make a new one for the new button.

Attaching the macro to a shortcut key
To "connect" this macro program to a shortcut key, launch the Customize dialog again (View - Toolbars - Customize), and select the Keyboard tab. Select Macros in the Category box, and select Macro in the Set shortcut key for box. In the Commands list select SortColumn, and then choose CTRL+ALT+S as the key sequence for executing the SortColumn macro program. To do this, click in the Press new shortcut key box, and then press CTRL+ALT+S; this key sequence will now be displayed in this box, and the Assign button will become available. Click the Assign button to finalize your choice.

After you Close this dialog, the SortColumn macro program can now be executed from the keyboard by pressing CTR+ALT+S.

Attaching the macro to a menu
To attach the macro program to a menu item, select the Commands/Macros tab on the Customize dialog. Select Macros in the Categories box, and the SortColumn macro will be displayed in the Commands box.

Click on the SortColumn macro, and drag it to the desired menu and menu location (e.g., the end of the Edit menu).

As you can see, the new macro was appended now as a menu item at the end of the Edit menu.