VBA Excel Object (XIX)

Important objects using VBA programming, users will be processed very little. Here are some common objects -

  • Application Object
  • Workbook object
  • Worksheet object
  • Objects range

Application Object

Application Object consists -

  • Application-wide settings and options.
  • Return to top-level object methods, for example ActiveCell, ActiveSheetand so on.

Examples

'Example 1 :
Set xlapp = CreateObject("Excel.Sheet") 
xlapp.Application.Workbooks.Open "C:\test.xls"

'Example 2 :
Application.Windows("test.xls").Activate

'Example 3:
Application.ActiveCell.Font.Bold = True

Workbook object

WorkbookObject is a Workbooksmember of a set, and included in the current Microsoft Excel all open in Workbookthe object.

Examples

'Ex 1 : To close Workbooks
Workbooks.Close

'Ex 2 : To Add an Empty Work Book
Workbooks.Add

'Ex 3: To Open a Workbook
Workbooks.Open FileName:="Test.xls", ReadOnly:=True

'Ex : 4 - To Activate WorkBooks
Workbooks("Test.xls").Worksheets("Sheet1").Activate

Worksheet object

Worksheet object is a member of the working set of tables and contains all the worksheets in the workbook objects.

Examples

'Ex 1 : To make it Invisible
Worksheets(1).Visible = False

'Ex 2 : To protect an WorkSheet
Worksheets("Sheet1").Protect password:=strPassword, scenarios:=True

Objects range

RangeObject represents a cell, row, or column comprising one or more consecutive units of cells selected cell block.

'Ex 1 : To Put a value in the cell A5
Worksheets("Sheet1").Range("A5").Value = "5235"

'Ex 2 : To put a value in range of Cells
Worksheets("Sheet1").Range("A1:A4").Value = 5

 

Guess you like

Origin www.cnblogs.com/sunyllove/p/11348375.html
Recommended