vb 引用excel 进行操作,第一次运行没有问题,第二次执行时出现错误
DimxlAppAsExcel.ApplicationDimxlBookAsExcel.WorkbookDimxlsheetAsExcel.WorksheetSetxlA...
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
set xlBook = xlApp.Workbooks.Open(App.Path & "\mb2.xls")
Set xlsheet = xlBook.Worksheets("Listado")
'......... 下面这段代码的问题,删掉这段代码,重复执行 没问题
If row_mb2 > 1 Then
xlsheet.Rows("1:5").Copy
For i = 1 To row_mb2 - 1
xlsheet.Rows(6 * i + 1 & ":" & 6 * i + 1).Select
ActiveSheet.Paste '这里报错
Next
'==========================
'.................................
xlApp.DisplayAlerts = False
str = Left(App.Path, Len(App.Path) - 12) & "\excel\MB2-" & Format(Date, "yyyymmdd") & Format(Time, "hhmmss") & ".xls"
xlBook.SaveAs str
Set xlsheet = Nothing
xlBook.Close True
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
'第二次运行时出现错误 实时错误'91'
'对像变量或with变量未设置....
'为什么会第二次报错呢,要怎么解决, excel 2007 展开
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
set xlBook = xlApp.Workbooks.Open(App.Path & "\mb2.xls")
Set xlsheet = xlBook.Worksheets("Listado")
'......... 下面这段代码的问题,删掉这段代码,重复执行 没问题
If row_mb2 > 1 Then
xlsheet.Rows("1:5").Copy
For i = 1 To row_mb2 - 1
xlsheet.Rows(6 * i + 1 & ":" & 6 * i + 1).Select
ActiveSheet.Paste '这里报错
Next
'==========================
'.................................
xlApp.DisplayAlerts = False
str = Left(App.Path, Len(App.Path) - 12) & "\excel\MB2-" & Format(Date, "yyyymmdd") & Format(Time, "hhmmss") & ".xls"
xlBook.SaveAs str
Set xlsheet = Nothing
xlBook.Close True
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
'第二次运行时出现错误 实时错误'91'
'对像变量或with变量未设置....
'为什么会第二次报错呢,要怎么解决, excel 2007 展开
1个回答
展开全部
oleExcel.Quit '退出Excel
Set xlsApp = Nothing
Set xlsBook = Nothing
Set xlsSheet = Nothing
End Sub
改为:
oleExcel.Quit '退出Excel
Set xlsSheet = Nothing
Set xlsBook = Nothing
Set xlsApp = Nothing
End Sub
Set xlsApp = Nothing
Set xlsBook = Nothing
Set xlsSheet = Nothing
End Sub
改为:
oleExcel.Quit '退出Excel
Set xlsSheet = Nothing
Set xlsBook = Nothing
Set xlsApp = Nothing
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询