Objects
- Updated2024-09-12
- 3 minute(s) read
Excel > Objects
Objects
The following topics describe the object-oriented script interface for working with Excel workbooks. You create the Workbook object with CreateExcelWorkbook. The CreateExcelWorkbook command creates a new Excel workbook based on an existing Excel file. If you do not specify an Excel file, the command creates an Excel workbook with an empty worksheet named Sheet1. You can use the interface to create and delete worksheets and to find cells in worksheets, read cell values, and edit cell contents.
The following example creates an Excel workbook, fills the first worksheet with random values, and saves this Excel workbook in a new XLSX file. If the value is 47, the example highlights this cell with a red background.
| VBScript | Python |
Dim iRow, jCol, oMyWorkbook, oMySheet, oMyCell Set oMyWorkbook = CreateExcelWorkbook() Set oMySheet = oMyWorkbook.Worksheets(1) For iRow = 1 to 1000 For jCol = 1 to 20 Set oMyCell = oMySheet.Cells(iRow, jCol) oMyCell.Value = Round(Random(100)) If oMyCell.Value = 47 THEN oMyCell.Interior.Color = RGB(255, 0, 0) End If Next Next Call oMyWorkbook.SaveAs(DataWritePath & "Test.xlsx")
The following example creates an Excel worksheet, fills the first worksheet with random values, searches for the value 47 in the worksheet, highlights the found cells with a red background, and saves the Excel workbook in a new XLSX file:
| VBScript | Python |
Dim iRow, jCol, oMyWorkbook, oMySheet, oMyCell Set oMyWorkbook = CreateExcelWorkbook() Set oMySheet = oMyWorkbook.Worksheets(1) For iRow = 1 to 1000 For jCol = 1 to 20 Set oMyCell = oMySheet.Cells(iRow, jCol) oMyCell.Value = Round(Random(100)) Next Next Set oMyCell = oMySheet.Find(47, eExcelSearchEquals, 0, 0) Do While Not(oMyCell Is Nothing) oMyCell.Interior.Color = RGB(255, 0, 0) Set oMyCell = oMySheet.Find(47, eExcelSearchEquals, oMyCell.Row, oMyCell.Column) Loop Call oMyWorkbook.SaveAs(DataWritePath & "Test.xlsx")
Display all
Hide all