Вы находитесь на странице: 1из 10

Excel

Excel Automation Object Model


Microsoft has developed the Excel application with heirarachy of object model.We can do excel operations using excel object model. Simple object model Example: Excel Application ! Wor"boo"s ! Wor"sheet ! cells

Create

an Excel File:

'Create a new Microsoft Excel object Set myxl = createobject("excel.application") 'To make Excel isible myxl.!pplication."isible = tr#e myxl.$orkbooks.!%% wait & 'Sa e t'e Excel file as (tp.xls myxl.!cti e$orkbook.Sa e!s ")*+(tp.xls" 'close Excel myxl.!pplication.,#it Set myxl=not'in Create

an Excel File , Enter some data , Save the Excel and close the Excel:

Set myxl = createobject("excel.application") 'Make s#re t'at yo# 'a e create% an excel file before exe#tint'e script. '.se t'e pat' of excel file in t'e below co%e '!lso make s#re t'at yo#r excel file is in Close% state before exe#tin- t'e script. myxl.$orkbooks./pen ")*+(tp.xls" myxl.!pplication."isible = tr#e 't'is is t'e name of S'eet in Excel file "(tp.xls" w'ere %ata nee%s to be entere% set mys'eet = myxl.!cti e$orkbook.$orks'eets("S'eet0")

'Enter al#es in S'eet0. 'T'e format of enterin- al#es in Excel is excelS'eet.Cells(row1col#mn)= al#e mys'eet.cells(010). al#e ="2ame" mys'eet.cells(01&). al#e ="!-e" mys'eet.cells(&10). al#e ="3am" mys'eet.cells(&1&). al#e ="&4" mys'eet.cells(510). al#e ="3a-'#" mys'eet.cells(51&). al#e ="06" 'Sa e t'e $orkbook Read the data from Excel File: Set myxl = createobject("excel.application") 'Make s#re t'at yo# 'a e create% an excel file before exe#tint'e script. '.se t'e pat' of excel file in t'e below co%e '!lso make s#re t'at yo#r excel file is in Close% state myxl.$orkbooks./pen ")*+(tp.xls" myxl.!pplication."isible = tr#e 't'is is t'e name of S'eet in Excel file "(tp.xls" w'ere %ata nee%s to be entere% set mys'eet = myxl.!cti e$orkbook.$orks'eets("S'eet0") '7et t'e max row occ#pie% in t'e excel file 3ow=mys'eet..se%3an-e.3ows.Co#nt '7et t'e max col#mn occ#pie% in t'e excel file Col=mys'eet..se%3an-e.col#mns.co#nt 'To rea% t'e %ata from t'e entire Excel file 8or i= 0 to 3ow 8or j=0 to Col Ms-box mys'eet.cells(i1j). al#e 2ext 2ext 'Sa e t'e $orkbook myxl.!cti e$orkbook.Sa e 'Close t'e $orkbook myxl.!cti e$orkbook.Close 'Close Excel myxl.!pplication.,#it

Set mys'eet =not'inSet myxl = not'in Compare

!o Excel sheets Cell b" cell:

Mismatc'=4 Set myxl = createobject("excel.application") 'To make Excel isible myxl."isible = Tr#e '/pen a workbook "(tp0.xls" Set $orkbook0= myxl.$orkbooks./pen("C*+(tp0.xls") '/pen a workbook "(tp&.xls" Set $orkbook&= myxl.$orkbooks./pen("C*+(tp&.xls") Set Set mys'eet0=$orkbook0.$orks'eets("S'eet0") mys'eet&=$orkbook&.$orks'eets("S'eet0")

'Compare two s'eets cell by cell 8or Eac' cell 9n mys'eet0..se%3an-e ':i-'li-'ts t'e cell if cell al#es not matc' 9f cell."al#e ;<mys'eet&.3an-e(cell.!%%ress)."al#e T'en ':i-'li-'ts t'e cell if cell al#es not matc' cell.9nterior.Color9n%ex = 5 mismatc'=0 En% 9f 2ext 9f Mismatc'=4 T'en Ms-box "2o Mismac' exists" En% 9f 'close t'e workbooks $orkbook0.close $orkbook&.close myxl.,#it set myxl=not'in Search

for #articular value in Excel:

Set myxl = createobject("excel.application")

'Make s#re t'at yo# 'a e create% an excel file before exe#tint'e script. '.se t'e pat' of excel file in t'e below co%e '!lso make s#re t'at yo#r excel file is in Close% state before exec#tin- t'e script. myxl.$orkbooks./pen ")*+(tp.xls" myxl.!pplication."isible = tr#e 'T'is is t'e name of S'eet in Excel file "(tp.xls" w'ere %ata nee%s to be entere% set mys'eet = myxl.!cti e$orkbook.$orks'eets("S'eet0") 'Contents of S'eet0 '2ame !-e '3am &4 '3a-'# 06 'Select t'e #se% ran-e in partic#lar s'eet $it' mys'eet..se%3an-e ' )ata "3am" to searc' ' =oop t'ro#-' t'e #se% ran-e 8or eac' searc'>%ata in mys'eet..se%3an-e ' compare wit' t'e expecte% %ata 9f searc'>%ata="3am" t'en 'make t'e cell wit' color if it fin%s t'e %ata searc'>%ata.9nterior.Color9n%ex = ?4 En% 9f next En% $it' 'Sa e t'e $orkbook myxl.!cti e$orkbook.Sa e 'Close t'e $orkbook myxl.!cti e$orkbook.Close 'Close Excel myxl.!pplication.,#it Set mys'eet =not'inSet myxl = not'in-

Cop"

an Excel sheet to another Excel sheet:

Set myxl = createobject("excel.application") 'To make Excel isible myxl."isible = Tr#e '/pen a workbook "(tp0.xls" Set $orkbook0= myxl.$orkbooks./pen("C*+(tp0.xls") '/pen a workbook "(tp&.xls" Set $orkbook&= myxl.$orkbooks./pen("C*+(tp&.xls") 'Copy t'e #se% ran-e of workbook "(tp0.xls" $orkbook0.$orks'eets("S'eet0")..se%3an-e.Copy '@aste t'e copie% al#es in abo e step in t'e !0 cell of workbook "(tp&.xls" $orkbook&.$orks'eets("S'eet0").3an-e("!0").@asteSpecial @aste =xl"al#es 'Sa e t'e workbooks $orkbook0.sa e $orkbook&.sa e 'close t'e workbooks $orkbook0.close $orkbook&.close myxl.,#it set myxl=not'in Addsheet

Method:

#escription: Adds the specified sheet to the run time #ata $able and returns the sheet so that you can directly set properties of the new sheet in the same statement. Syntax: #ata$able.AddSheet%Sheet&ame' Example:

'Create a %atatable s'eet %#rin- 3#n time.T'is s'eet will be a ailable %#rin- r#n time only. '$e can iew t'is s'eet in 3es#lt S#mmary#n%er section "3#n Time %ata Table". %atatable.!%%S'eet(",tpworl%") 'To a%% col#mn name an% a %efa#lt al#e #n%er t'em. %atatable.7etS'eet(",tpworl%").!%%@arameter "name"1"3am"

%atatable.7etS'eet(",tpworl%").!%%@arameter "a-e"1"0A" wait 6


$eleteSheet

Method:

#escription: #eletes the specified sheet from the run time #ata $able. Syntax: #ata$able.#eleteSheet Sheet(# Example:

'Create a %atatable s'eet %#rin- 3#n time.T'is s'eet will be a ailable %#rin- r#n time only. '$e can iew t'is s'eet in 3es#lt S#mmary #n%er section "3#n Time %ata Table" . %atatable.!%%S'eet(",tpworl%") 'To %elete %atatable s'eet %atatable.)eleteS'eet(",tpworl%") %atatable.)eleteS'eet("7lobal") wait 5
%mport

Method:

#escription: (mports the specified Microsoft Excel file to the run time #ata $able. Syntax: #ata$able.(mport%)ile&ame' Example given below

Export

Method:

#escription: Saves a copy of the run time #ata $able in the specified location. Syntax: #ata$able.Export%)ile&ame' Example:

'9f %ata is store% in m#ltiple s'eet in external Excel $orkbook 1 'we can import m#ltiple s'eet %ata into )atatable an% ')o neccessary operation on t'e importe% %ata. %atatable.9mport "C*+(tptest.xls" 'To -et t'e total co#nt of ,T@ %atatable s'eets ms-box %atatable.7etS'eetCo#nt

'!fter t'e operations are %one1yo# can export t'e all t'e (tp %atas'eets to t'e External file 'Create a %atatable s'eet %#rin- 3#n time.T'is s'eet will be a ailable %#rin- r#n time only. %atatable.Export "C*+(tptest.xls" '$e can iew t'is s'eet in 3es#lt S#mmary Time %ata Table" . %atatable.!%%S'eet(",tpworl%") 'To %elete %atatable s'eet %atatable.)eleteS'eet(",tpworl%") %atatable.)eleteS'eet("7lobal") wait 5
&alue

#n%er section "3#n

#ropert":

#escription: #ata$able default property. *etrieves or sets the value of the cell in the specified parameter and the current row of the run time #ata $able. Syntax: #ata$able.+alue%,arameter(# -. Sheet(#/' Example given below

%mportSheet

Method:

#escription: (mports a sheet of a specified file to a specified sheet in the run time #ata $able. $he data in the imported sheet replaces the data in the destination sheet %see Sheet#est argument'. Syntax: #ata$able.(mportSheet%)ile&ame. SheetSource. Sheet#est' Example given below

ExportSheet

Method:

#escription: Exports a specified sheet of the run time #ata $able to the specified file. (f the specified file does not exist. a new file is created and the specified sheet is saved.(f the current file exists. but the file does not contain a sheet with the specified sheet name. the sheet is inserted as the last sheet of the file. (f the current file exists and the file contains the specified sheet. the exported sheet overwrites the existing sheet. Syntax: #ata$able.ExportSheet%)ile&ame. #$Sheet' Example:

'9f %ata is store% in a partic#lar s'eet in external Excel $orkbook 1 'we can import only t'at partic#lar s'eet %ata into )atatable an%

'%o neccessary operation on t'e importe% %ata. 'Create a s'eet "S'eet0" in %atatable.!%%S'eet "S'eet0" (tp

'S'eet0 %ata from excel file contains t'e followin- %ata '2ame !-e '3am# &4 '3akes' &? '9mport S'eet0 %ata from excel file to (tp s'eet "S'eet0" %atatable.9mportS'eet "C*+(tps'eet.xls"1"S'eet0"1"S'eet0" '!%% a col#mn "3es#lt" for %isplayin- res#lt in (tp s'eet %atatable.7etS'eet("S'eet0").!%%@arameter "3es#lt"1"" wait & '!pply t'e lo-ic* if a-e is less t'an 0A t'en Minor" else "Major" t'e -#y is "

row =%atatable.7etS'eet("S'eet0").7et3owCo#nt 8or i = 0 to row %atatable.7etS'eet("S'eet0").SetC#rrent3ow(i) 9f %atatable."al#e("!-e"1"S'eet0") < 0A T'en %atatable."al#e("3es#lt"1"S'eet0") = "Major" Else %atatable."al#e("3es#lt"1"S'eet0") = "Minor" En% 9f 2ext 'Export t'e (tp s'eet "S'eet0" bak to external )atatable.ExportS'eet "C*+(tps'eet.xls"1"S'eet0" excel

' !fter exportin- yo# can see t'at t'e excel file now 'as been #p%ate% wit' res#lt
'etSheet

Method:

#escription: *eturns the specified sheet from the run time #ata $able. Syntax: #ata$able.0etSheet%Sheet(#' Example given below

'etSheetCount

Method:

#escription: *eturns the total number of sheets in the run time #ata $able. Syntax: #ata$able.0etSheet1ount Example given below

'etCurrentRo!

Method:

#escription: *eturns the current %active' row in the first sheet in the run time #ata $able %global sheet'. Syntax: #ata$able.0et1urrent*ow Example given below

'etRo!Count

Method:

#escription: *eturns the total number of rows in the longest column in the first sheet in the run time #ata $able %global sheet'. Syntax: #ata$able.0et*ow1ount Example given below

SetCurrentRo!

Method:

#escription: Sets the specified row as the current %active' row in the run time #ata $able. Syntax: #ata$able.Set1urrent*ow%*ow&umber' Example:

'Create a %atatable s'eet %#rin- 3#n time. 'T'is s'eet will be a ailable %#rin- r#n time only. '$e can iew t'is s'eet in 3es#lt S#mmary #n%er section "3#n Time %ata Table" . %atatable.!%%S'eet(",tpworl%") 'To a%% col#mn name an% a %efa#lt al#e #n%er t'em.

%atatable.7etS'eet(",tpworl%").!%%@arameter "name"1"3am" %atatable.7etS'eet(",tpworl%").!%%@arameter "a-e"1"0A" 'Enter %ata into secon% row of %atats'eet ",tpworl%" %atatable.7etS'eet(",tpworl%").SetC#rrent3ow(&) %atatable."al#e("name"1",tpworl%")="3am#" %atatable."al#e("a-e"1",tpworl%")="&5" 'total n#mber of %atas'eets in t'e r#nBtime )ata Table Ms-box %atatable.7etS'eetCo#nt

'7et t'e max #se% ran-e of t'e %atas'eet row=%atatable.7etS'eet(",tpworl%").7et3owCo#nt '=oop to rea% all t'e %ata in t'e %atas'eet ",tpworl%" 8or )row= 0 to row %atatable.7etS'eet(",tpworl%").SetC#rrent3ow()row) Ms-box %atatable."al#e("name"1",tpworl%") Ms-box %atatable."al#e("a-e"1",tpworl%") Ms-box "C#rrent 3ow is* " C %atatable.7etS'eet(",tpworl%").7etC#rrent3ow 2ext
'lobalSheet

#ropert":

#escription: *eturns the first sheet in the run time #ata $able %global sheet'. Syntax: #ata$able.0lobalSheet Example given below

(ocalSheet

#ropert":

#escription: *eturns the current %active' local sheet of the run time #ata $able. Syntax: #ata$able.2ocalSheet Example:

'To a%% col#mn name in 7lobal S'eet an% a %efa#lt t'em. %atatable.7lobalS'eet.!%%@arameter "name"1"ram#" %atatable.7lobalS'eet.!%%@arameter "a-e"1"0A" 'To a%% col#mn name in =ocal S'eet an% a %efa#lt t'em. %atatable.=ocalS'eet.!%%@arameter "name"1"3akes'" %atatable.=ocalS'eet.!%%@arameter "a-e"1"&&" wait 6

al#e #n%er

al#e #n%er

Вам также может понравиться