Ok I have this problem, when I am trying to retrieve data from a rs232 device through winwedge , if the button is pressed accidentally the data field from the device is empty I get run time error 5 type mismatch. I want the macro to not run if it detects zero or an empty field.
I posted in another forum and they offered:
but i still get the same error.
code is:
I posted in another forum and they offered:
Code:
if sDat = "" then exit sub
but i still get the same error.
code is:
Code:
Private Sub Aluminum_Click()
Dim R As Long
Dim X As Long
Dim Chan As Long
Dim NumFields As Long
Dim vDat As Variant
Dim sDat As String
' find the next empty row in Column A
R = ThisWorkbook.Sheets("DDE").Cells(65000, 1).End(xlUp).Row + 1
' Establish DDE link to WinWedge on Com1
Chan = DDEInitiate("WinWedge", "Com3")
' How many data fields are we retrieving from WinWedge?
NumFields = 1
' Loop through all data fields defined in the Wedge
For X = 1 To NumFields
' Request the data from each field in the wedge
vDat = DDERequest(Chan, "Field(" & CStr(X) & ")")
' Convert the data from a variant array data type to a string
sDat = vDat(1)
If sDat=0 or sDat ="" then GoTo CloseSub
' Place the data in cell location Row = R, Column = X
ThisWorkbook.Sheets("DDE").Cells(R, X + 1).Value = sDat
Next
DDETerminate Chan ' Close the DDE channel
' Insert a date/time stamp in the sheet in the same row as the data
ThisWorkbook.Sheets("DDE").Cells(R, NumFields + 3).Value = Now
Sheets("DDE").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = "Aluminum"
Sheets("DDE").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Sheets("User").Range("b3").Value
CloseSub:
DDETerminate Chan ' Close the DDE channel
End Sub
Last edited: