如何使用VB6将工作表添加到Excel文件 [英] how to add sheets to excel file using vb6
本文介绍了如何使用VB6将工作表添加到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用vb6将工作表添加到excel文件中
以下代码是添加一张纸,但是当我添加一张以上的纸时,会出现错误
i want to add sheets to excel file using vb6
the following code is add a sheet but when i add more than one sheet it gives error
Dim xl As Excel.Application
Set xl = CreateObject("excel.Application")
xl.Workbooks.Open (fname)
xl.Worksheets.Add After:=Worksheets(1) '', Count:=1, Type:=xlWorksheet
code = "ASR/" & Text1.Text & "/" & Text2.Text & "/" & Text3.Text & "/" & Text4.Text
nocode = txtnocode.Text
heading = Text6.Text
xl.Range("A1").Value = heading
For i = 2 To nocode + 1
xl.Cells(i, 1).Value = code & "/" & i - 1 ''"ORG"
Next
ActiveWorkbook.Save
Excel.ActiveWindow.Close
xl.Quit
Set xl = Nothing
MsgBox ("Done")
推荐答案
您需要以与创建工作簿相同的方式来创建工作表对象.
You need to create the worksheet object in the same manner that you created the workbook.
Dim ExcelApp As Excel.Application
Dim ExcelWorkbook As Excel.Workbook
Dim ExcelSheet As Excel.Worksheet
Set ExcelApp = Excel.Application
Set ExcelWorkbook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelWorkbook.Worksheets.Add
ExcelSheet.Name = "New_Sheet_Name " & CStr(Unique_ID)
Unique_ID:每个工作表都需要使用唯一的名称进行标识.
还请确保您处置对象,否则在程序关闭后Excel应用程序将继续运行.
Unique_ID: each sheet needs to be identified with a unique name.
Also assure that you dispose the objects or the Excel application will keep running after your program closes.
Set ExcelSheet = Nothing
Set ExcelWorkbook = Nothing
Set ExcelApp = Nothing
这篇关于如何使用VB6将工作表添加到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文