Questions? Search our site or call us at 1-800-722-6004

Insert Data into Top Row and Move Previous Readings Down

WinWedge DDE Server Settings

Remember to set WinWedge up in DDE Server Mode! Click here.

DDE Application Name: Excel
DDE Topic: System
DDE Command: [RUN("InsertDataOnTop")]

Excel Instructions

Create or edit a macro module in Excel and enter the following code in the module: (To create a new module click on Insert > Module from the Menus in the Visual Basic Editor Window.)

This example is a variation of the first example (GetSingleField). It is modified to always insert the newest record at the top - thus shifting all previous records down a row.

Sub InsertDataOnTop()
' Inserts data on the top of previous records starting at Row 3 (to allow for Column Labels). If
' you want to start on a different row, please adjust the R variable.

    Dim Chan As Long, vDat As Variant, sDat As String, R As Long
    R = 3 ' Change this value to log data to a different row.
    On Error Resume Next ' ignore errors
    ThisWorkbook.Sheets("Sheet1").Rows(R).Insert ' Insert a row above R
    Chan = DDEInitiate("WinWedge", "COM1") ' Connect to WinWedge on COM1
    vDat = DDERequest(Chan, "Field(1)") ' Get Field(1)
    sDat = vDat(1) ' convert to a string
    ThisWorkbook.Sheets(1).Cells(R, 1).Value = sDat 'Log to R
    DDETerminate Chan ' Terminate the DDE link
End Sub

The example above sets up WinWedge to issue a DDE command consisting of the Excel "RUN" command forcing Excel to run the subroutine "InsertDataOnTop()" after each data record is received from your serial device. The "InsertDataOnTop" subroutine performs a DDERequest to WinWedge that returns the contents of FIELD(1) to a variable named "vDat". vDat is then converted to a string variable, sDat and then is inserted at the top of a column and moves all previous readings down in the sheet sort of like a strip chart recorder. The latest data is always in the specified with all previous readings below it scrolling down as each new reading is inputted. The example above assumes that the open workbook contains a worksheet named Sheet1.

Categories: Macro / Code Sample, Microsoft Excel, Data Collection, Serial (RS232), WinWedge

Last Updated: 2014.06.06

Need more help?

Don't hesitate to call or email us with your questions

Our office is open 9AM - 5PM Monday Through Friday (E.S.T.)

Technical Support: 215-496-0202

Toll-Free: 1 (800) 722-6004
Skype: taltech1 (Voice only)