首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

vb写入excel的有关问题,

2012-02-16 
vb写入excel的问题,急,在线等怎么以追加的方式向excel中写入数据,并且自动保存,急,在线等,知道教下,谢谢[

vb写入excel的问题,急,在线等
怎么以追加的方式向excel中写入数据,并且自动保存,急,在线等,知道教下,谢谢

[解决办法]
Dim exApp As Excel.Application
Private Sub Command1_Click()
On Error GoTo g_error
CommonDialog1.Filter = "Excel文件(*.xls)|*.xls "
CommonDialog1.ShowOpen
If Len(CommonDialog1.FileName) > = 1 Then
exApp.Workbooks.Open CommonDialog1.FileName
Dim last_row
last_row = 0
For i = Sheets(1).UsedRange.Count To 1 Step -1
If Len(Range( "a " & i)) > = 1 Then last_row = i: Exit For
Next
Range( "a " & last_row + 1) = "aaaaa "
Range( "b " & last_row + 1) = "bbbbb "
Range( "c " & last_row + 1) = "ccccc "
Range( "d " & last_row + 1) = "ddddd "
exApp.ActiveWorkbook.Save
exApp.ActiveWorkbook.Close
End If
MsgBox "数据追加成功! "
Exit Sub
g_error:
MsgBox "数据追加失败! "
End Sub

Private Sub Form_Load()
Set exApp = New Excel.Application
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
exApp.Quit
Set exApp = Nothing
End Sub

热点排行