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 SubPrivate Sub Form_Load() Set exApp = New Excel.Application End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) exApp.Quit Set exApp = Nothing End Sub
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 SubPrivate Sub Form_Load()
Set exApp = New Excel.Application
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
exApp.Quit
Set exApp = Nothing
End Sub