'#####################################
'@author huyphungvan@gmail.com
'@description: VBS - Excel - Worksheet
'#####################################
Const xlExcel8 = 56
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
' Create a new workbook => 1 worksheet: Sheet1
Set objWorkbook = objExcel.Workbooks.Add
Set objSheet = objWorkbook.Worksheets.Item(1)
Dim i
For i = 1 To 20 Step 2
objSheet.Cells(i, 3) = "Pair " & i
objSheet.Cells(i, 4) = "Element 1"
objSheet.Cells(i + 1, 4) = "Element 2"
With objSheet
.Range("C" & i & ":C" & (i+1)).MergeCells = True
.Range("C" & i & ":C" & (i+1)).Font.Bold = True
.Range("C" & i & ":C" & (i+1)).HorizontalAlignment = -4108 'xlVAlignCenter
.Range("C" & i & ":C" & (i+1)).VerticalAlignment = -4108
End With
Next
' Create hyperlink with formula
objSheet.Cells(5,1).Formula = "=HYPERLINK(""#Sheet1!A1"",""Link to Sheet1"")"
' If sheet name contains SPACE character, don't forget add single quote '
objSheet.Cells(7,"A").Formula = "=HYPERLINK(""#'Second Sheet'!B2"",""Link to Second Sheet"")"
' Create more worksheet
Set objSheet = objWorkbook.Worksheets.Add
objSheet.Name = "Second Sheet"
objSheet.Cells(1, "A") = "Hello world"
' Move worksheet to last
objSheet.Move , objWorkbook.Sheets(objWorkbook.Worksheets.Count)
' Save workbook
objWorkbook.SaveAs "C:\vbscript\excel\example.xls", xlExcel8
objWorkbook.Close
' Close excel application
objExcel.Quit
|
visual basic script - create worksheet - excel |