7,763
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Xls As Excel.Application, xBook As Excel.Workbook, xSht As Excel.Worksheet
Private bFile As String
Private Sub Command1_Click()
''开始工作.....
On Error Resume Next
Set Xls = New Excel.Application
bFile = "e:\123.xls"
Set xBook = Xls.Workbooks.Open(bFile)
If xBook Is Nothing Then
Set xBook = Xls.Workbooks.Add
xBook.SaveAs bFile
End If
If xBook Is Nothing Then
MsgBox "存在未知错误!无法正常打开或创建工作簿!"
Exit Sub
End If
''判断工作表数量
If xBook.Worksheets.Count < 2 Then
''如果工作表数量不够,不管3721添加2个
xBook.Worksheets.Add
xBook.Worksheets.Add
End If
''设置2个表头
xBook.Worksheets(1).Range("a1") = "时间"
xBook.Worksheets(2).Range("a1") = "时间"
Set xSht = xBook.Worksheets(1) ''第一次用工作表1
''设置定时器
Timer1.Interval = 5000
Timer1.Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not xBook Is Nothing Then xBook.Close True
Set xBook = Nothing
If Not Xls Is Nothing Then Xls.Quit
Set Xls = Nothing
End Sub
Private Sub Timer1_Timer()
Dim i As Long
i = xSht.Range("a1").End(xlDown).Row
If i = xSht.Rows.Count Then i = 1
xSht.Range("a" & (i + 1)) = Timer ''暂时用时间值作为数据存储
''更改xhst
Set xSht = xBook.Worksheets(IIf(xSht Is xBook.Worksheets(1), 2, 1))
xBook.Save
End Sub