Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(txtFile.Text)
Set xlSheet = xlBook.Sheets(1)
xlSheet.Range("A1:J1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
xlApp.Visible = True
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(txtFile.Text)
Set xlSheet = xlBook.Sheets(1)
xlSheet.Range("A1:J1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
运行到第二个 With Selection
.HorizontalAlignment = xlCenter
时报错:实时错误“91”,对象变量或with块变量未设置
其中 第二个Selection=nothing
12 个解决方案
#1
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
都关闭excell了
Set xlBook = Nothing
Set xlSheet = Nothing
都关闭excell了
#2
眼误,那几句没问题。
#3
up
#4
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
#5
能说一下是什么原因吗?
#6
实际程序中我不希望关闭excel
因为要由用户预览后选择是否打印
因为要由用户预览后选择是否打印
#7
With Selection --> With xlSheet.Selection
#8
同意楼上的
#9
你没有完全关闭EXCEL,在进程中你还可以看到的。应该:
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
#10
leolan(史留香) :
xlSheet.Selection 报错 未找到方法或数据成员
xlSheet.Selection 报错 未找到方法或数据成员
#11
試試 xlApp.Selection
#12
你把xlBook 和xlSheet 在用第二个时候都Nothing了,所以在用第二个之前必需在
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
#1
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
都关闭excell了
Set xlBook = Nothing
Set xlSheet = Nothing
都关闭excell了
#2
眼误,那几句没问题。
#3
up
#4
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
#5
能说一下是什么原因吗?
#6
实际程序中我不希望关闭excel
因为要由用户预览后选择是否打印
因为要由用户预览后选择是否打印
#7
With Selection --> With xlSheet.Selection
#8
同意楼上的
#9
你没有完全关闭EXCEL,在进程中你还可以看到的。应该:
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
#10
leolan(史留香) :
xlSheet.Selection 报错 未找到方法或数据成员
xlSheet.Selection 报错 未找到方法或数据成员
#11
試試 xlApp.Selection
#12
你把xlBook 和xlSheet 在用第二个时候都Nothing了,所以在用第二个之前必需在
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet