此代码使用一个工作表创建一个Excel文件.此表包含我创建并且工作正常的项目代码(ASR / Floor / Dept./ Item_Name / Item_details / 1),但我想在此Excel文件中添加工作表以创建另一个项目代码,然后保存这个文件.
Dim xlApp As Excel.Application Dim wb As Workbook Dim ws As Worksheet Dim var As Variant Dim code As String Dim i,nocode As Integer Dim fname,heading As String code = "ASR/" & Text1.Text & "/" & Text2.Text & "/" & Text3.Text & "/" & Text4.Text Set xlApp = New Excel.Application Set wb = xlApp.Workbooks.Add ' Create a new WorkBook Set ws = wb.Worksheets("Sheet1") 'Specify your worksheet name nocode = txtnocode.Text heading = Text6.Text For i = 2 To nocode + 1 ws.Cells(i,1).Value = code & "/" & i - 1 '"ORG" Next i fname = "c:\" & Text5.Text & ".xls" wb.SaveAs (fname) wb.Close xlApp.Quit Set ws = Nothing Set wb = Nothing Set xlApp = Nothing
解决方法
您正在寻找Worksheets.Add方法:
wb.WorkSheets.Add().Name = "SecondSheet"
请参阅MSDN(向下滚动并展开“表格和工作表”),了解可以为其添加的不同参数.添加包括能够在特定页面之前或之后添加表格.