VB自动保存EXCEL
各位好,我现在想用VB打开一个现有的EXCEL,并往里面写数据,写一行保存一行,XECEL要不可见,应该怎么写,现在我写这个是直接新建一个EXCEL(可见),然后MSCOMM触发一次就写一行数据,可是不能保存。Option Explicit
Dim xlapp As Variant
Dim xlBook As Variant
Dim xlsheet As Variant
Dim i As Variant
Private Sub Command1_Click()
End
End Sub
Private Sub Form_Load()
Set xlapp = CreateObject("excel.application")
xlapp.Visible = True
Set xlBook = xlapp.Workbooks.Add
Set xlsheet = xlBook.Worksheets(1)
MSComm1.Settings = "9600,n,8,1"
= 3
MSComm1.NullDiscard = False
MSComm1.RThreshold = 62
MSComm1.InputMode = 0
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
xlapp.Quit '关闭EXCEL
Set xlapp = Nothing '释放EXCEL对象
End Sub
Private Sub MSComm1_OnComm()
Text1 = ""
If i = 0 Then
i = 2
Else
i = i + 1
End If
Text1.Text = MSComm1.Input
Text2.Text = Mid(Text1, 6, 10) '日期
Text3.Text = Mid(Text1, 17, 8) '时间
Text4.Text = Mid(Text1, 32, 6) '压力
Text5.Text = Mid(Text1, 39, 3) '压力单位
Text6.Text = Mid(Text1, 44, 2) '测试结果
Text7.Text = Mid(Text1, 48, 5) '泄漏量
Text8.Text = Mid(Text1, 54, 8) '泄漏单位
xlsheet.Cells(i, 2) = Text2.Text
xlsheet.Cells(i, 3) = Text3.Text
xlsheet.Cells(i, 4) = Text4.Text
xlsheet.Cells(i, 5) = Text5.Text
xlsheet.Cells(i, 6) = Text6.Text
xlsheet.Cells(i, 7) = Text7.Text
xlsheet.Cells(i, 8) = Text8.Text
xlsheet.Cells(1, 1) = "序号"
xlsheet.Cells(1, 2) = "日期"
xlsheet.Cells(1, 3) = "时间"
xlsheet.Cells(1, 4) = "测试压力"
xlsheet.Cells(1, 5) = "单位"
xlsheet.Cells(1, 6) = "测试结果"
xlsheet.Cells(1, 7) = "泄露量"
xlsheet.Cells(1, 8) = "单位"
xlsheet.Cells(i, 1) = 0 + i
End Sub