my code stock.com

VBA code to acquire data from CLX to Excel

Snippet options

Download: Download snippet as clx-to-xl.txt.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

Private Sub CommandButton1_Click()

    rslinx = OpenRSLinx() 'Open connection to RSlinx

    'Loop through reading the CLX array tags and
    'put the values into cells
    For i = 0 To 28
        For j = 0 To 11
        'First the array of REALs
        'Get the value form the DDE link
        'MyRange = Range("A" & (i + 1) & "")
        'MyRange.Select
        realdata = DDERequest(rslinx, "P6316_Slope_Resistance[" & i & "," & j & "],L1,C1")
        
        'If there is an error, display a message box
        If TypeName(data) = "Error" Then
            If MsgBox("Error reading tag P6316_Slope_Resistance[" & i & "]. " & _
                "Continue with Read?", vbYesNo + vbExclamation, _
                "Error") = vbNo Then Exit For
        Else
            'No error, place data in cell
    
            Cells(7 + i, j + 1) = realdata
        End If
        
        'Now the array of DINTs
        'Get the value from the DDE link
        'dintdata = DDERequest(rslinx, "DINT_Array[" & i & "],L1,C1")
        
        'If there is an error, display a message box
        'If TypeName(data) = "Error" Then
            'If MsgBox("Error reading tag DINT_Array[" & i & "]. " & _
                '"Continue with Read?", vbYesNo + vbExclamation, _
                '"Error") = vbNo Then Exit For
        'Else
            'No error, place data in cell
            'Cells(2 + i, 5) = dintdata
        'End If
        Next j
    Next i
   
   For k = 0 To 28
    For l = 0 To 1
        realdata = DDERequest(rslinx, "Program:Resistance.LR[" & k & "," & l & "],L1,C1")
        
        If TypeName(data) = "Error" Then
            If MsgBox("Error reading tag LR[" & k & "]. " & _
                "Continue with Read?", vbYesNo + vbExclamation, _
                "Error") = vbNo Then Exit For
        Else
            'No error, place data in cell
    
            Cells(40 + k, l + 1) = realdata
        End If
    Next l
    Next k
    
    SumX = DDERequest(rslinx, "Program:Resistance.Sum_X],L1,C1")
    SumY = DDERequest(rslinx, "Program:Resistance.Sum_Y],L1,C1")
    SumXY = DDERequest(rslinx, "Program:Resistance.Sum_XY],L1,C1")
    SumX2 = DDERequest(rslinx, "Program:Resistance.Sum_X2],L1,C1")
    SumY2 = DDERequest(rslinx, "Program:Resistance.Sum_Y2],L1,C1")
    
    Cells(39, 8) = SumX
    Cells(40, 8) = SumY
    Cells(41, 8) = SumXY
    Cells(42, 8) = SumX2
    Cells(43, 8) = SumY2
    
    'Terminate the DDE connection
    DDETerminate rslinx

End Sub

Private Function OpenRSLinx()
    On Error Resume Next
    
    'Open the connection to RSLinx
    OpenRSLinx = DDEInitiate("RSLINX", "Pot_6316")
    
    'Check if the connection was made
    If Err.Number <> 0 Then
        MsgBox "Error Connecting to topic", vbExclamation, "Error"
        OpenRSLinx = 0 'Return false if there was an error
    End If
    
End Function

Private Sub CommandButton2_Click()

    rslinx = OpenRSLinx() 'Open connection to RSlinx

    'Loop through the cells and write values to the CLX array tags
    For i = 0 To 9

        'First the array of REALs
        'Get the value from the DDE link
        realdata = DDERequest(rslinx, "REAL_Array[" & i & "],L1,C1")
        'If there is an error, display a message box
        If TypeName(data) = "Error" Then
            If MsgBox("Error reading tag REAL_Array[" & i & "]. " & _
                "Continue with write?", vbYesNo + vbExclamation, _
                "Error") = vbNo Then Exit For
        Else
            'No error, place data in CLX
            DDEPoke rslinx, "REAL_Array[" & i & "]", Cells(2 + i, 4)
        End If
        
        'Now the array of DINTs
        'Get the value from the DDE link
        dintdata = DDERequest(rslinx, "DINT_Array[" & i & "],L1,C1")
        'If there is an error, display a message box
        If TypeName(data) = "Error" Then
            If MsgBox("Error reading tag DINT_Array[" & i & "]. " & _
                "Continue with write?", vbYesNo + vbExclamation, _
                "Error") = vbNo Then Exit For
        Else
            'No error, place data in CLX
            DDEPoke rslinx, "DINT_Array[" & i & "]", Cells(2 + i, 5)
        End If
    Next i
    
    'Terminate the DDE connection
    DDETerminate rslinx
End Sub

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.