如何将VB中的Text显示的数据存入到Excel中,并在后面加上存入数据时的时间?
1个回答
展开全部
窗口添加2个按钮,1个文本框,一个Timer( interval=10000 )
工程---引用----Microsoft Excel
输入以下代码:
Dim VBExcel As Object
Private Sub Form_Load()
Set VBExcel = CreateObject("Excel.Application")
End Sub
Private Sub Command1_Click()‘打开,写入并保存
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
FileName = "C:\Documents and Settings\Administrator\桌面\a.xls"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application") '判断Excel是否打开
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.Visible = False '设置EXCEL对象不可见
End If
If Dir(FileName) = "" Then '判断文件是否存在
MsgBox FileName & "未找到!", vbOKOnly, "友情提示"
Exit Sub
End If
Set xlBook = xlApp.Workbooks.Open(FileName)
Set xlsheet = xlApp.Worksheets(1)
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = Text1.Text '给单元格1行驶列赋值
xlBook.Save
End Sub
Private Sub Command2_Click()
With VBExcel
.Workbooks.Close'关闭工作表
End With
VBExcel.Quit
End Sub
Private Sub Timer1_Timer()'没10秒打开一次,保存一次,关闭一次i
Command1_Click
Command2_Click
End Sub
工程---引用----Microsoft Excel
输入以下代码:
Dim VBExcel As Object
Private Sub Form_Load()
Set VBExcel = CreateObject("Excel.Application")
End Sub
Private Sub Command1_Click()‘打开,写入并保存
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
FileName = "C:\Documents and Settings\Administrator\桌面\a.xls"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application") '判断Excel是否打开
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.Visible = False '设置EXCEL对象不可见
End If
If Dir(FileName) = "" Then '判断文件是否存在
MsgBox FileName & "未找到!", vbOKOnly, "友情提示"
Exit Sub
End If
Set xlBook = xlApp.Workbooks.Open(FileName)
Set xlsheet = xlApp.Worksheets(1)
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = Text1.Text '给单元格1行驶列赋值
xlBook.Save
End Sub
Private Sub Command2_Click()
With VBExcel
.Workbooks.Close'关闭工作表
End With
VBExcel.Quit
End Sub
Private Sub Timer1_Timer()'没10秒打开一次,保存一次,关闭一次i
Command1_Click
Command2_Click
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询