在Excel中添加新工作表(受vbscript控制)

时间:2015-01-19 04:47:47

标签: excel vbscript

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkBook = objExcel.Workbooks.Add()
i=4
objExcel.cells(1,1) = "Test1"
objExcel.cells(1,1).Font.Bold = True
objExcel.cells(2,1) = "Steps No"
objExcel.cells(2,1).Font.Bold = True
objExcel.cells(2,2) = "Test Steps"
objExcel.cells(2,2).Font.Bold = True
objExcel.cells(2,3) = "Expected Result"
objExcel.cells(2,3).Font.Bold = True
objExcel.cells(2,4) = "Remarks"
objExcel.cells(2,4).Font.Bold = True

'Need to add a new sheet in the excel and add content to it

objWorkBook.SaveAs(strFile)
objExcel.Quit 

我正在尝试创建Excel电子表格并向其添加数据。默认情况下,内容将在第一张表中更新。需要将内容添加到第二张表。 不确定如何在第二张表中添加数据。

1 个答案:

答案 0 :(得分:3)

像这样的东西

Set objWorkbook = objExcel.Workbooks.Add(1)
添加单页工作簿

Set objWorkSheet = objWorkbook.Sheets.Add

一起使用的第二张表

更短的版本

Dim objExcel, objWorkbook, objWorkSheet
 Set objExcel = CreateObject("Excel.Application")
 objExcel.Visible = True
 Set objWorkbook = objExcel.Workbooks.Add(1)
 Set objWorkSheet = objWorkbook.Sheets.Add

 i = 4
 With objWorkSheet
        .Cells(1, 1) = "Test1"
        .Cells(1, 1).Font.Bold = True
        .Range("A2:D2") = Array("Steps No", "Test Steps", "Expected Result", "Remarks")
        .Range("A2:D2").Font.Bold = True
 End With

完整代码

 Dim objExcel, objWorkbook, objWorkSheet
 Set objExcel = CreateObject("Excel.Application")
 objExcel.Visible = True
 Set objWorkbook = objExcel.Workbooks.Add(1)
 Set objWorkSheet = objWorkbook.Sheets.Add

 i = 4
 With objWorkSheet
    .Cells(1, 1) = "Test1"
    .Cells(1, 1).Font.Bold = True
    .Cells(2, 1) = "Steps No"
    .Cells(2, 1).Font.Bold = True
    .Cells(2, 2) = "Test Steps"
    .Cells(2, 2).Font.Bold = True
    .Cells(2, 3) = "Expected Result"
    .Cells(2, 3).Font.Bold = True
    .Cells(2, 4) = "Remarks"
    .Cells(2, 4).Font.Bold = True
 End With