Macro

Statistica's macro document is similar to other Statistica documents in that all its features are available via automation. This enables you to open and run other macros from a macro, along with passing arguments between them.

As an example, say that you have an SVB macro that subsets the active spreadsheet. It will base this subset on whether the values of the first variable contains text similar to "fail", and then exports it as an HTML file. This macro looks like this:

Option Base 1
Option Explicit
Sub Main
    Dim spr As Spreadsheet
    Set spr = ActiveSpreadsheet.Subset("",  _
                "v1 LIKE 'FAIL%'")
    spr.Header.Value = "Failure report"
    spr.SaveAs("C:\Data\Failure Report.htm", True)
    Set spr = Nothing
End Sub

Now, let us say that you have another macro that needs to perform this action with a particular spreadsheet. One option is to copy and paste the above into a new macro and rewrite it as a separate function. However, the drawback to this approach is that future improvements made to the original macro will not be reflected in the newer one. A better method would be to run the original macro from the newer one and pass a spreadsheet to it. This way, you would always be using the current version of the original macro. To do this, you need to augment the first macro so that it can accept an argument, as demonstrated here:

Option Base 1
Option Explicit
Sub Main
    Dim spr As Spreadsheet
    Set spr = GetScriptArgument().Subset("",  _
                "v1 LIKE 'FAIL%'")
    spr.Header.Value = "Failure report"
    spr.SaveAs("C:\Data\Failure Report.htm", True)
    Set spr = Nothing
End Sub

The adjustment that we made was to change ActiveSpreadsheet to GetScriptArgument(). The function GetScriptArgument retrieves the argument passed to the macro. This argument is a variant, meaning that you can pass any data type to the macro. In this case, you are passing a spreadsheet object.

Note: If nothing is passed to the macro, then GetScriptArgument returns the active spreadsheet.

Now, in your second macro you open the first macro to use it. In a new macro, enter the following:

Option Base 1
Option Explicit
Sub Main
    Dim mc As Macro
    Set mc = Macros.Open("C:\Macros\SubsetFailures.svb")
End Sub

Assuming that the first macro was saved as C:\Macros\SubsetFailures.svb, then this macro creates a Macro object and set it to this file.

Finally, we use our Macro object, which is pointing to an open instance of C:\Macros\SubsetFailures.svb, to run it. The Macro object provides a regular Execute function that simply runs the macro, but here we want to pass an argument to it. In this case, we use the ExecuteWithArgument function to do this.

mc.ExecuteWithArgument(Spreadsheets.Open( _
        Application.Path & "\Examples\Datasets\Adstudy.sta") )
This opens the example dataset Adstudy and pass it to SubsetFailures.svb. This makes SubsetFailures.svb use Adstudy as its input, rather than the active spreadsheet.