|
各位大神,我VBA小白,参考网上资料,做了一个从串口读取内容,记录下时间和内容的代码,但是还需要每读取一次内容和之前的比较是否有重复,重复则提示内容重复,否则才记录下来,不知怎么做,请大家帮忙,谢谢!
代码如下:
Private Sub CommandButton1_Click()
MkDir ThisWorkbook.Path & "\" & Format(Now(), "yyyymmddhhmm")
ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Now(), "yyyymmddhhmm") & "\" & Format(Now(), "yyyymmddhhmm") & ".xls"
Application.Quit
End Sub
Private Sub Label1_Click()
End Sub
Private Sub Label2_Click()
End Sub
Private Sub MSComm1_OnComm()
Dim t1 As Long, com_String As String
Static i As Integer
t1 = Timer
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.RThreshold = 0
Do
DoEvents
Loop While Timer - t1 < 0.05
com_String = MSComm1.Input
MSComm1.RThreshold = 1
i = i + 1: If i > 10 Then i = 1
Application.Cells(i + 1, "B").Value = com_String
Cells(i + 1, "A") = Format(Now(), "yyyy-mm-dd,hh:mm:ss")
Me.Label3.Caption = i
If i = 10 Then
MkDir ThisWorkbook.Path & "\" & Format(Now(), "yyyymmddhhmm")
ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Now(), "yyyymmddhhmm") & "\" & Format(Now(), "yyyymmddhhmm") & ".xls"
Range("a2:a30").Clear
Range("b2:b30").Clear
End If
End Select
End Sub
Private Sub iniMscomm()
'On Error Resume Next
MSComm1.CommPort = 15
MSComm1.Settings = "38400,N,8,1"
MSComm1.PortOpen = True
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeText
MSComm1.RTSEnable = True
MSComm1.InBufferCount = 0
End Sub
Private Sub UserForm_initialize()
iniMscomm
ActiveSheet.Cells.Clear
Cells(1, "A") = Format("时间")
Cells(1, "B") = Format("记录")
Me.Label1.Caption = ("请开始扫描!")
Me.Label1.FontSize = 50
Me.Label2.Caption = ("记录数量:")
Me.Label2.FontSize = 20
Me.Label3.Caption = ("0")
Me.Label3.FontSize = 20
Me.Label3.ForeColor = &HC000&
Me.CommandButton1.Caption = ("退出保存")
Me.CommandButton1.FontSize = 20
End Sub
|
|