VBS操作Excel常见方法.doc

上传人:scccc 文档编号:13945828 上传时间:2022-01-27 格式:DOC 页数:7 大小:101.50KB
返回 下载 相关 举报
VBS操作Excel常见方法.doc_第1页
第1页 / 共7页
VBS操作Excel常见方法.doc_第2页
第2页 / 共7页
VBS操作Excel常见方法.doc_第3页
第3页 / 共7页
VBS操作Excel常见方法.doc_第4页
第4页 / 共7页
VBS操作Excel常见方法.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《VBS操作Excel常见方法.doc》由会员分享,可在线阅读,更多相关《VBS操作Excel常见方法.doc(7页珍藏版)》请在三一文库上搜索。

1、VBS 操作 Excel 常见方法 _933VBS 操作 Excel 常见方法dim oExcel,oWb,oSheetSet oExcel= CreateObject(Excel.Application)Set oSheet = oWb.Sheets(Sheet1)MsgBox oSheet.Range(B2).Value #其他 新装电话表 .xls)提取单元格 B2 内容3 、如果是 XP 系统,可以使用如下代码Dim objFileDlgSet objFileDlg = CreateObject(UserAccounts.CommonDialog) objFileDlg.Filter

2、= Excel File (*.xls) |*.xlsIf objFileDlg.ShowOpen Thenmsgbox 您选择的文件是: & objFileDlg.FileName & vbCrLf End IfVBS 控制 Excel 的一些常见方法:( 一 ) 使用动态创建的方法首先创建 Excel 对象,使用 ComObj:oExcel = CreateObject( Excel.Application )1) 显示当前窗口: oExcel.Visible = True2) 更改 Excel 标题栏: oExcel.Caption = 应用程序调用 Microsoft Excel3)

3、添加新工作簿: oExcel.WorkBooks.Add4) 打开已存在的工作簿: oExcel.WorkBooks.Open( C:ExcelDemo.xls )5) 设置第 2 个工作表为活动工作表: oExcel.WorkSheets(2).Activate 或oExcel.WorksSheets( Sheet2 ).Activate6) 给单元格赋值: oExcel.Cells(1,4).Value = 第一行第四列 7) 设置指定列的宽度(单位:字符个数),以第一列为例:oExcel.ActiveSheet.Columns(1).ColumnsWidth = 58) 设置指定行的高度

4、(单位:磅)(1磅二0.035厘米),oExcel.ActiveSheet.Rows(2).RowHeight = 1/0.035 19) 在第 8 行之前插入分页符: oExcel.WorkSheets(1).Rows(8).PageBreak = 110) 在第 8 列之前删除分页符: oExcel.ActiveSheet.Columns(4).PageBreak = 011) 指定边框线宽度:oExcel.ActiveSheet.Range( B3:D4 ).Borders(2).Weight = 31- 左 2- 右 3- 顶 4- 底 5- 斜( ) 6-斜( / )12) 清除第一

5、行第四列单元格公式: oExcel.ActiveSheet.Cells(1,4).ClearContents13) 设置第一行字体属性: oExcel.ActiveSheet.Rows(1).Font.Name = 隶书 oExcel.ActiveSheet.Rows(1).Font.Color = clBlue oExcel.ActiveSheet.Rows(1).Font.Bold = True oExcel.ActiveSheet.Rows(1).Font.UnderLine = True14) 进行页面设置:a. 页眉:oExcel.ActiveSheet.PageSetup.Cent

6、erHeader = b. 页脚: oExcel.ActiveSheet.PageSetup.CenterFooter = c. 页眉到顶端边距 2cm : oExcel.ActiveSheet.PageSetup.HeaderMargin = 2/0.035d. 页脚到底端边距 3cm: oExcel.ActiveSheet.PageSetup.HeaderMargin = 3/0.035e. 顶边距 2cm : oExcel.ActiveSheet.PageSetup.TopMargin = 2/0.035f. 底边距 2cm : oExcel.ActiveSheet.PageSetup.

7、BottomMargin = 2/0.035g. 左边距 2cm : oExcel.ActiveSheet.PageSetup.LeftMargin = 2/0.035h. 右边距 2cm : oExcel.ActiveSheet.PageSetup.RightMargin = 2/0.035i. 页面水平居中: oExcel.ActiveSheet.PageSetup.CenterHorizontally = 2/0.035j. 页面垂直居中: oExcel.ActiveSheet.PageSetup.CenterVertically = 2/0.035k. 打印单元格网线: oExcel.

8、ActiveSheet.PageSetup.PrintGridLines = True15) 拷贝操作:a. 拷贝整个工作表:以第二行为例:厘米报表演示第&P页oExcel.ActiveSheet.Used.Range.Copyb. 拷贝指定区域: oExcel.ActiveSheet.Range( A1:E2 ).Copyc. 从 A1 位置开始粘贴: oExcel.ActiveSheet.Range.( A1 ).PasteSpeciald. 从文件尾部开始粘贴: oExcel.ActiveSheet.Range.PasteSpecial16) 插入一行或一列:a. oExcel.Acti

9、veSheet.Rows(2).Insertb. oExcel.ActiveSheet.Columns(1).Insert17) 删除一行或一列:a. oExcel.ActiveSheet.Rows(2).Deleteb. oExcel.ActiveSheet.Columns(1).Delete18) 打印预览工作表: oExcel.ActiveSheet.PrintPreview19) 打印输出工作表: oExcel.ActiveSheet.PrintOut20) 工作表保存:if not oExcel.ActiveWorkBook.Saved then oExcel.ActiveSheet

10、.PrintPreview21) 工作表另存为:oExcel.SaveAs( C:ExcelDemo1.xls )22) 放弃存盘: oExcel.ActiveWorkBook.Saved = True23) 关闭工作簿:oExcel.WorkBooks.Close24) 退出 Excel :oExcel.Quit( 二 ) 使用 VBS 控制 Excle 二维图1) 选择当第一个工作薄第一个工作表 set oSheet=oExcel.Workbooks(1).Worksheets(1)2) 增加一个二维图 achart=oSheet.chartobjects.add(100,100,200,

11、200)3) 选择二维图的形态 achart.chart.charttype=44) 给二维图赋值set series=achart.chart.seriescollection range=sheet1!r2c3:r3c9 series.add range,true二维图II5) 加上二维图的标题 achart.Chart.HasTitle=True achart.Chart.ChartTitle.Characters.Text= Excle6) 改变二维图的标题字体大小 achart.Chart.ChartTitle.Font.size=187) 给二维图加下标说明achart.Chart

12、.Axes(xlCategory, xlPrimary).HasTitle = Trueachart.Chart.Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text =下标说明”8) 给二维图加左标说明achart.Chart.Axes(xlValue, xlPrimary).HasTitle = Trueachart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text=左标说明9) 给二维图加右标说明achart.Chart.Axes(xlValue, xlSec on dar

13、y).HasTitle = Trueachart.Chart.Axes(xlValue,xlSecondary).AxisTitle.Characters.Text =右标说明10 )改变二维图的显示区大小achart.Chart.PlotArea.Left = 5achart.Chart.PlotArea.Width = 223achart.Chart.PlotArea.Height = 108如何用vbs把excel的单元格数据写到txt复制代码代码如下:If WScript.Argume nts.Cou nt 0 The n File name =WScript.Argume nts(O

14、)Set a = CreateObject(Excel.Applicatio n)If File name = The nFile name = a.GetOpe nF ile name(Excel Files (*.xls), *.xls)If VarType(File name) = vbBoolea n ThenMsgBox Excel2Txt 用于将Excel文件的每个Sheet保存为一个文本文件。& vbCr & vbLf & vbCr & vbLf & 用法:Excel2Txt file name.xls或在对话框中打开Excel文件。WScript.QuitEnd IfEnd I

15、fSet w = a.Workbooks.Ope n( File name)n = Replace(Replace(w.Name, .xls, ), .XLS,)a.DisplayAlerts = FalseFor Each s In w.Sheetss.SaveAs w.Path & & n & _ & s.Name & .txt, 20Nexta.Quit把以上代码存为Excel2Txt.vbs双击执行就行了VBS 操作 Excel复制代码代码如下:Set objExcel = CreateObject(Excel.Application)建一个 exel 对象Set objWorkboo

16、k = objExcel.Workbooks.Ope n _领域模型.xls) 打开文件取 sheet 数量 新建 sheet 对象 遍历 sheet- Generated by ScriptGenerator - & vbcrlf & & vbcrlf & vbcrlfCount = objWorkbook.WorkSheets.Count Set my = CreateObject(Excel.Sheet) For Each my In objWorkbook.WorkSheets If my.Name = 目录 or my.Name = SecondHandHouse Thendo no

17、thingElse Wscript.Echo my.Name 获得 sheet 名字Wscript.Echo my.Rows.CountstrToBeWrited = strToBeWrited & create table & my.Name & vbcrlfstrToBeWrited = strToBeWrited & /*= =*/ & vbcrlfstrToBeWrited = strToBeWrited & /* Table: & my.Name & */ & vbcrlf strToBeWrited = strToBeWrited & /*= =*/ & vbcrlfstrToBe

18、Writed = strToBeWrited & create table & my.Name & (& vbcrlf rowNum = 3Do Until my.Cells(rowNum,1).Value = Wscript.Echo sAMAccountName: & my.Cells(rowNum, 2).Value strToBeWrited = strToBeWrited & & my.Cells(rowNum,2).Value & & my.Cells(rowNum,3).Value & not nullIf not my.Cells(rowNum,9).Value = Then

19、strToBeWrited = strToBeWrited & default & my.Cells(rowNum,9).ValueEnd If strToBeWrited = strToBeWrited & , & vbcrlfrowNum = rowNum + 1LoopstrToBeWrited = strToBeWrited & constraint PK_ & my.Name & primary key (id) & vbcrlfstrToBeWrited = strToBeWrited & ) & vbcrlfEnd IfstrToBeWrited = strToBeWrited

20、& vbcrlfNextFor Each my In objWorkbook.WorkSheets 遍历 sheetIf my.Name = 目录 or my.Name = SecondHandHouse Thendo nothingElsestrToBeWrited = strToBeWrited & ” con strai nt PK_ & my.Name & ” primary key (id) & vbcrlfstrToBeWrited = strToBeWrited & ) & vbcrlfEnd IfstrToBeWrited = strToBeWrited & vbcrlfNex

21、t写文件set fs =createobject(scripti ng.filesystemobject)set f = fs.ope ntextfile(E:DOCHewldbscript.sql,2, true)Wscript.Echo strToBeWritedf.write strToBeWritedf.closeSet f = nothingSet fs = nothingobjExcel.Quit 结束退出 复制代码代码如下:Dim ExcelSet Excel = CreateObject(Excel.Applicatio n)不显示提示信息,这样保存的时候就不会提示是否要覆盖原

22、文件Excel.DisplayAlerts=FALSE调用EXCEL文件的时候不显示Excel.visible=FALSEExcel.workbooks.ope n( D:test.XLS)将sheet1 设置为活动sheetExcel.workbooks(1).activate插入行,这条我找MSDN都没找到,最后乱试试出来的Excel.ActiveSheet.rows(1).i nsertExcel.ActiveSheet.Cells(1,1).Value = DateExcel.ActiveSheet.Cells(1,2).Value = row1Excel.ActiveSheet.Ce

23、lls(1,3).Value = comme nt1Excel.ActiveSheet.rows(2).i nsertExcel.ActiveSheet.Cells(2,1).Value = DateExcel.ActiveSheet.Cells(2,4).Value = row2Excel.ActiveSheet.Cells(2,7).Value = comme nt2Excel.saveExcel.quitSet Excel = Nothi ngExcel.ActiveSheet.rows(1).i nsert不用找MSDN在EXCEL帮助中就能找到,看 编程信息” / “Microsof

24、t Excel方法Visual Basic 参考” /属性” / “-R / “Rows属性”的介绍,和 编程信 息” / “Microsoft Excel Visual Basic参考” / 方法” / “-L” / “Insert的介绍,就能明白这条语句的语法。因为在EXCEL的VBA中,“Rows “ColumnS、“Cells ”属性返回的都是 Range对 象,所以对它们的应用可以等同 Range 对象的应用。例如:你在 EXCEL 的 VBA 编辑器中可以这样写 cells(1,1).value=abccells(1,1).wraptext=false在写这些语句时,你应该注意到,在写完“cells(1,1).” 之后,并没有弹出应该弹出的属性/ 方法列表,但是这些语句确实可以正常运行。我的方法是:在EXCEL中录制宏,然后在EXCEL的VBA编辑器中修改语句,调试运行 无误后再粘贴到 VBS 语句中,进行适当的修改。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1