Professional Documents
Culture Documents
A VBScript16
A VBScript16
A VBScript16
Session 16
1
Last time we’ve learned
Regulars Expressions.
Methods and properties.
How to use the object and his
collections.
How to create complex patterns.
2
Subjects for session 16
Microsoft Excel Object Model.
Workbooks and Worksheets
Ranges and Cells.
Worksheet Functions.
3
Microsoft Excel Object Model
file:///D:/MIQTP/Class/Images/ExcelObjectModel1.bmp
file:///D:/MIQTP/Class/Images/ExcelLegend.bmp
4
Microsoft Excel Object Model
file:///D:/MIQTP/Class/Images/excelobjectModel2.bmp
file:///D:/MIQTP/Class/Images/ExcelLegend.bmp
5
Workbooks
Use the Workbooks property of the “Excel.Application”
object to return the Workbooks collection.
Dim objAppXL
Set objAppXL = CreateObject(“Excel.Application”)
objAppXL.Visible = False
With objAppXL.Workbooks
.Add ‘--- Create an new empty workbook.
.Open “bugs.xls” ‘--- Open an existing workbook.
.Close ‘--- Close all open workbook.
End With
objAppXL.Quit
Set objAppXL = Nothing
6
Workbooks
Use the Workbooks collection with an index or a name to
reference a workbook.
7
Workbooks
Use the Workbook – Example
objAppXL.Workbooks.Open “\\zeppo\FT_QA\bugs.xls”
objAppXL.Workbooks.SaveAs “C:\bugs_local.xls”
objAppXL.ActiveWorkbook.Close
objAppXL.Workbooks.Add.Activate
objAppXL.ActiveWorkbook.Subject = “sales 2004”
8
WorkSheets
Use the Worksheets property of the workbook collection to
return the Worksheets collection.
9
WorkSheets
Use the Worksheets collection with an index or a name to
reference a worksheet.
10
Worksheets
Use the Worksheet – Example
Dim objWshXL
Set objWshXL = objAppXL.ActiveWorkbook.Worksheets(1)
‘--- Perform a calculation.
objWshXL.Range(“A1:A3”) = “1”
objWshXL.Range(“A4”) = “=Sum(A1:A3)”
objWshXL. PrintOut
11
Ranges and Cells
You may want to reference cells or a range of cells for doing
something such entering a formula or changing the format.
A Range can be also a single cell.
Example
‘--- Show the number of worksheets in the workbook.
objWshXL.Range(“A1:A3”).Font.Bold = True
12
Ranges and Cells
Range(“A1”) Cell A1.
Range(“A1:B5”) Cells A1 thorough B5.
Range(“C5:D9,G9:H16”) A multiple area selection.
Range(“A:A”) Column A.
Range(“1:1”) Row 1.
Range(“A:C”) Column A thorough C.
Range(“1:5”) Rows 1 thorough 5.
Range(“1:1,3:3,8:8”) Rows 1,3, and 8.
Range(“A:A,C:C,F:F”) Columns A,C, and
13
Ranges and Cells
Referencing cells:
Dim i
For i = 1 to 20
objWshXL.Cells(i,3) = i
Next
14
Ranges and Cells
Referencing cells:
Dim i
For i = 1 to 20
objWshXL.Cells(i,3) = i
Next
15
Ranges and Cells
Referencing rows and columns:
Rows(1) Row one.
Rows All the rows on the worksheet.
Columns(1) Column one.
Columns(“A”) Column one.
Columns All the columns in the worksheet.
Example
16
Ranges and Cells
Referencing a named range:
‘--- Font all range cells red color.
objWshXL.Range(“salaries”).Font.Color = RGB(255,0,0)
17
Ranges and Cells
Looping through a range of cells:
‘--- Go over all cells in a range.
For Each c In objWshXL.Range(“A1:D10”).Cells
if Abs(c.Value) = 0.01 Then c.Value = 0
Next
Selecting cells:
objWshXL.Range(“A1”).Select
objWshXL.ActiveCell = “Hello”
objWshXL.Range(“A1”) = “Hello”
18
Ranges and Cells
Useful methos and properties
Methods Properties
Activate Cells
CheckSpelling Columns
Clear Rows
ClearContent Font
ClearFormats Formula
Copy Style
Cut Value
Paste
Delete
Replace
Select
Sort
19
Ranges and Cells
Used as a container for Microsoft Excel worksheet functions that
can be called from VBScript.
Example:
‘--- Find the minimum value within a range.
Dim theRange, minVal
20
Ranges and Cells
Useful worksheets functions
Methods
Count
Max
Min
Sum
And
Or
Power
Round
StDev
Average
Pi
And Many More
21
Make sure to visit us
Tutorials
Articles
Proikects
And much more
www.AdvancedQTP.com
22