ignore null or zero data in rs232 connection/channel

errtu

Board Regular
Joined
Sep 23, 2010
Messages
134
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:

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:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Rich (BB code):
Private Sub GetIt_Click()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Dim R As Long<o:p></o:p>
Dim X As Long<o:p></o:p>
Dim Chan As Long<o:p></o:p>
Dim NumFields As Long<o:p></o:p>
Dim vDat As Variant<o:p></o:p>
Dim sDat As String<o:p></o:p>
' find the next empty row in Column A<o:p></o:p>
R =ThisWorkbook.Sheets("DDE").Cells(65000, 1).End(xlUp).Row + 1<o:p></o:p>
<o:p></o:p>
' Establish DDE link to WinWedge on Com1    <o:p></o:p>
<o:p></o:p>
Chan = DDEInitiate("WinWedge", "Com3")<o:p></o:p>
<o:p></o:p>
' How many data fields are we retrieving from WinWedge?     <o:p></o:p>
<o:p></o:p>
NumFields = 1<o:p></o:p>
<o:p></o:p>
' Loop through all data fields defined in the Wedge     <o:p></o:p>
<o:p></o:p>
For X = 1 To NumFields<o:p></o:p>
<o:p></o:p>
 ' Request the data from each field in the wedge<o:p></o:p>
 vDat = DDERequest(Chan, "Field(" & CStr(X) & ")")<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
 ' Convert the data from a variant array data type to a string<o:p></o:p>
<o:p></o:p>
 sDat = CStr(vDat(1))<o:p></o:p>
  <o:p></o:p>
If sDat = "0" Or sDat= "" Then<o:p></o:p>
Goto CloseDDE<o:p></o:p>
End if<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
 ' Place the data in cell location Row = R, Column = X<o:p></o:p>
<o:p></o:p>
ThisWorkbook.Sheets("DDE").Cells(R, X + 1).Value = sDat<o:p></o:p>
<o:p></o:p>
Next<o:p></o:p>
CloseDDE:<o:p></o:p>
<o:p></o:p>
DDETerminate Chan ' Close the DDE channel<o:p></o:p>
<o:p></o:p>
' Insert a date/time stamp in the sheet in the same row as the data     <o:p></o:p>
<o:p></o:p>
ThisWorkbook.Sheets("DDE").Cells(R, NumFields + 3).Value = Now<o:p></o:p>
<o:p></o:p>
Sheets("DDE").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = "UBC"<o:p></o:p>
Sheets("DDE").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Sheets("User").Range("b3").Value<o:p></o:p>
<o:p></o:p>
End Sub <o:p></o:p>

Try testing Vdat for NULL before attempting to convert using Cstr
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top