Excel VBA + VM167 and 2 inductive proximity sensors

I’m working on the following project using Excel (VBA) and a VM167 card. On channel 1 there is a inductive proximity sensor and on channel 2 there is also a inductive proximity sensor.
Since I’m not expert I will try to explain my concept.

When proximity sensor 1 is ON (channel 1 value is high) it has to update row X cell 1 in the Excel sheet with the value 1. At the same time the program should check if sensor 2 is ON (channel 2 value is high). If channel 2 is also high at the same time the program should update row X, cell 2 with the value 1. If not it has to update row X cell 2 with 0.
When sensor 1 is OFF again (channel 1 value is low) it has to stop the check if sensor 2 is ON and will jump to the next row.
Then the procedure will start again.

The interval (in time) between ON and OFF (high or low value of the channel) of sensor 1 is not defined upfront, the event is random.

Any help would be very appreciated!

Attached a VBA code to read the digital inputs 1 and 2.
The VBA code will do following:
First it connects to the VM167 card and clears columns 1 and 2 on the 20 first rows.
Then it starts to wait until channel 1 value goes high.
When high, the ‘1’ is printed to column 1.
The sate of channel 2 is now continuously checked while the channel 1 is high.
If channel 2 was also high (at any moment), the value ‘1’ will be printed to column 2.
If channel 2 was all the time low, the value ‘0’ will be printed to column 2.
When sensor 1 is OFF again (channel 1 value is low) it stops the check if sensor 2 is ON and will jump to the next row.

You can exit the code loop by pulling any digital input 3-8 high.
If any problems are encountered, you can close and restart the Excel.

You can use the VM167Demo.xls as “starting point”.
In the Excel workbook delete all except the two buttons.
Then press F11 and replace the original VBA code with this one:[code]Option Explicit
Private Declare Function OpenDevices Lib “vm167.dll” () As Long
Private Declare Sub CloseDevices Lib “vm167.dll” ()
Private Declare Sub InOutMode Lib “vm167.dll” (ByVal CardAddress As Long, ByVal HighNibble As Long, ByVal LowNibble As Long)
Private Declare Function ReadAllDigital Lib “vm167.dll” (ByVal CardAddress As Long) As Long

Dim Row As Long
Dim InDigital As Long
Dim Sensor2 As Long
Dim CardAddress As Long
Dim Cards As Long

Sub Button1_Click()
Range(“A1:B21”).Select
Selection.ClearContents
Cards = OpenDevices()
Select Case Cards
Case 0
ActiveSheet.Cells(1, 8) = “Card open error.”
Case 1
ActiveSheet.Cells(1, 8) = “Card 0 connected.”
CardAddress = 0
Case 2
ActiveSheet.Cells(1, 8) = “Card 1 connected.”
CardAddress = 1
Case 3
ActiveSheet.Cells(1, 8) = “Cards 0 and 1 connected.”
CardAddress = 0
Case -1
ActiveSheet.Cells(1, 8) = “Card not found.”
End Select
If Cards > 0 Then
Row = 0
Do While Row < 20
InDigital = ReadAllDigital(CardAddress)
If InDigital > 3 Then
GoTo exit_loop
End If
If (InDigital And 1) = 1 Then
Row = Row + 1
ActiveSheet.Cells(Row, 1) = 1

            Sensor2 = 0
            Do While (InDigital And 1) = 1
                If (InDigital And 2) = 2 Then
                    Sensor2 = 1
                End If
                InDigital = ReadAllDigital(CardAddress)
                If InDigital > 3 Then
                    GoTo exit_loop
                End If
            Loop
            ActiveSheet.Cells(Row, 2) = Sensor2
        End If
    Loop
End If

exit_loop:
End Sub

Sub Button2_Click()
CloseDevices
ActiveSheet.Cells(1, 8) = “Card closed”
End Sub[/code]

Thank you so much. I will test this code the following days.