VB 打开已有excel、修改、保存,出错提示“该文件已存在,是否要覆盖” 源代码如下,如何解决?
PrivateSubCommand1_Click()DimiAsVariant'DimxlAppAsExcel.Application'DimxlBookAsExcel....
Private Sub Command1_Click()
Dim i As Variant
' 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("d:\大宋银行.xls", 3, False)
Set xlsheet = xlBook.Worksheets(1)
xlsheet.Activate
xlsheet.cells(1, 1) = "顾客"
xlsheet.cells(1, 2) = "金额"
i = 1
Do While xlsheet.cells(i, 1) <> ""
i = i + 1
Loop
xlsheet.cells(i, 1) = a
xlsheet.cells(i, 2) = 0
xlApp.activeWorkBook.save
xlBook.Close
xlApp.quit
Set xlsheet = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
Form3.Show
End Sub
代码前3句如果去掉 ’ 的话, 就提示第一句用户类未定义,真搞不懂 不定义没事,一定义反而出错,VB真是够奇怪的,但是一些老工程师还爱用,参加工作不学还不行
a 是全局变量 无需再定义 展开
Dim i As Variant
' 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("d:\大宋银行.xls", 3, False)
Set xlsheet = xlBook.Worksheets(1)
xlsheet.Activate
xlsheet.cells(1, 1) = "顾客"
xlsheet.cells(1, 2) = "金额"
i = 1
Do While xlsheet.cells(i, 1) <> ""
i = i + 1
Loop
xlsheet.cells(i, 1) = a
xlsheet.cells(i, 2) = 0
xlApp.activeWorkBook.save
xlBook.Close
xlApp.quit
Set xlsheet = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
Form3.Show
End Sub
代码前3句如果去掉 ’ 的话, 就提示第一句用户类未定义,真搞不懂 不定义没事,一定义反而出错,VB真是够奇怪的,但是一些老工程师还爱用,参加工作不学还不行
a 是全局变量 无需再定义 展开
1个回答
展开全部
dim ex as object
Set ex = CreateObject("Excel.Application")
ex.Visible = False
Dim fname As String
fname = "d:\大宋银行.xls"
ex.Workbooks.Open (fname)
ex.cells(1, 1) = "顾客"
ex.cells(1, 2) = "金额"
.....
把你的 xlsheet都改为ex
....
ex.DisplayAlerts = False '关闭提示
ex.ActiveWorkbook.SaveAs( "d:\大宋银行.xls")
ex.Quit
Set ex = Nothing
Set ex = CreateObject("Excel.Application")
ex.Visible = False
Dim fname As String
fname = "d:\大宋银行.xls"
ex.Workbooks.Open (fname)
ex.cells(1, 1) = "顾客"
ex.cells(1, 2) = "金额"
.....
把你的 xlsheet都改为ex
....
ex.DisplayAlerts = False '关闭提示
ex.ActiveWorkbook.SaveAs( "d:\大宋银行.xls")
ex.Quit
Set ex = Nothing
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询