19.2.2. Method 2 - Writing Excel macros that use the DDEPoke command

Writing an Excel macro is perhaps the most flexible and efficient way to send data from Excel to the DataHub instance. By using the DDEPoke command in an Excel macro you have complete control over exactly when the data is transmitted. We will also explain how you can write an Excel macro to transmit multiple points at the same time (see Additional Pointers for more details).

In our example, we have chosen to 'add a control button' to run the macro, but you could also run your macro on a timed interval to produce an automatic update on a cycle that you control.

19.2.2.1. Create a macro

  1. Open a spreadsheet.

  2. From the Tools menu, select Macro, and then Macros....

  3. In the Macro Name: field of the Macro dialog box, type the name SendOutput, and press the Create button.

  4. In the Visual Basic text entry window that comes up, edit the macro to read as follows:

    '
    ' SendOutput Macro
    '
    Sub SendOutput()
        mychannel = DDEInitiate("datahub", "default")
        Application.Worksheets("Sheet1").Activate
        Call DDEPoke(mychannel, "my_pointname", Cells(4, 3))
        DDETerminate mychannel
    End Sub
    [Note]

    Use the name of your data point from the DataHub instance for my_pointname.

    [Note]

    We use cell C4 in this example. If you need to use another cell, you will have to replace (4, 3) with the row and column numbers of the cell you wish to use. You can also name a range to send multiple values as an array.

  5. Save and close the Visual Basic text entry window.