在 Excel 中添加新的 sheet(vbscript 控制)

Add new sheet in Excel (vbscript controlled)

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 传播sheet 并向其中添加数据。默认情况下,内容会在第一个 sheet 中更新。需要添加内容到第二个sheet。 不确定如何在第二个 sheet.

中添加数据

像这样

Set objWorkbook = objExcel.Workbooks.Add(1)
添加一个 sheet 工作簿

Set objWorkSheet = objWorkbook.Sheets.Add
第二 sheet 与

一起工作

更短的版本

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