用vb写excel文件时出错,求帮助!(急!)
下面是我写的一个函数,把数据写到一个excel模板文件中,但是每次写完一次后,退出这个函数,第二次再进入这个函数时,usedlines = Range(Cells(1, 1), ActiveSheet.UsedRange).Rows.Count行就出错,提示:Object variable or with block variale not set.不知道是为什么?
Function writeXLSFile(ByVal tablename As String)
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim usedlines As Integer
Dim writeline As Integer
Dim i As Integer
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
If Dir(App.path & "\" & tablename & ".xls") = "" Then
FileCopy App.path & "\Model\model.xls", App.path & "\" & tablename & ".xls"
End If
Set xlBook = xlApp.Workbooks.Open(App.path & "\" & tablename & ".xls")
Set xlSheet = Application.ActiveSheet
usedlines = Range(Cells(1, 1), ActiveSheet.UsedRange).Rows.Count
writeline = usedlines + 1
xlSheet.Cells(writeline, 1) = TxtPicture.Text
For i = 1 To 41
xlSheet.Cells(writeline, i + 1) = data(i)
Next i
xlApp.ActiveWorkbook.Save
xlApp.ActiveWorkbook.Close
xlApp.Application.Quit
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
End Function