




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
VBS操作Excel常见方法dim oExcel,oWb,oSheet Set oExcel= CreateObject(Excel.Application) Set oWb = oExcel.Workbooks.Open(E:其他新装电话表.xls) Set oSheet = oWb.Sheets(Sheet1) MsgBox oSheet.Range(B2).Value #提取单元格B2内容 . 3、如果是XP系统,可以使用如下代码 Dim objFileDlg Set objFileDlg = CreateObject(UserAccounts.CommonDialog) objFileDlg.Filter = Excel File (*.xls) |*.xls If objFileDlg.ShowOpen Then msgbox 您选择的文件是: & objFileDlg.FileName & vbCrLf End If VBS控制Excel的一些常见方法: (一) 使用动态创建的方法 首先创建 Excel 对象,使用ComObj: oExcel = CreateObject( Excel.Application ) 1) 显示当前窗口: oExcel.Visible = True 2) 更改 Excel 标题栏: oExcel.Caption = 应用程序调用 Microsoft Excel 3) 添加新工作簿: oExcel.WorkBooks.Add 4) 打开已存在的工作簿: oExcel.WorkBooks.Open( C:ExcelDemo.xls ) 5) 设置第2个工作表为活动工作表: oExcel.WorkSheets(2).Activate 或 oExcel.WorksSheets( Sheet2 ).Activate 6) 给单元格赋值: oExcel.Cells(1,4).Value = 第一行第四列 7) 设置指定列的宽度(单位:字符个数),以第一列为例: oExcel.ActiveSheet.Columns(1).ColumnsWidth = 5 8) 设置指定行的高度(单位:磅)(1磅0.035厘米),以第二行为例: oExcel.ActiveSheet.Rows(2).RowHeight = 1/0.035 1厘米 9) 在第8行之前插入分页符: oExcel.WorkSheets(1).Rows(8).PageBreak = 1 10) 在第8列之前删除分页符: oExcel.ActiveSheet.Columns(4).PageBreak = 0 11) 指定边框线宽度: oExcel.ActiveSheet.Range( B3:D4 ).Borders(2).Weight = 3 1-左 2-右 3-顶 4-底 5-斜( ) 6-斜( / ) 12) 清除第一行第四列单元格公式: oExcel.ActiveSheet.Cells(1,4).ClearContents 13) 设置第一行字体属性: 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 = True 14) 进行页面设置: a.页眉: oExcel.ActiveSheet.PageSetup.CenterHeader = 报表演示 b.页脚: oExcel.ActiveSheet.PageSetup.CenterFooter = 第&P页 c.页眉到顶端边距2cm: oExcel.ActiveSheet.PageSetup.HeaderMargin = 2/0.035 d.页脚到底端边距3cm: oExcel.ActiveSheet.PageSetup.HeaderMargin = 3/0.035 e.顶边距2cm: oExcel.ActiveSheet.PageSetup.TopMargin = 2/0.035 f.底边距2cm: oExcel.ActiveSheet.PageSetup.BottomMargin = 2/0.035 g.左边距2cm: oExcel.ActiveSheet.PageSetup.LeftMargin = 2/0.035 h.右边距2cm: oExcel.ActiveSheet.PageSetup.RightMargin = 2/0.035 i.页面水平居中: oExcel.ActiveSheet.PageSetup.CenterHorizontally = 2/0.035 j.页面垂直居中: oExcel.ActiveSheet.PageSetup.CenterVertically = 2/0.035 k.打印单元格网线: oExcel.ActiveSheet.PageSetup.PrintGridLines = True 15) 拷贝操作: a.拷贝整个工作表: oExcel.ActiveSheet.Used.Range.Copy b.拷贝指定区域: oExcel.ActiveSheet.Range( A1:E2 ).Copy c.从A1位置开始粘贴: oExcel.ActiveSheet.Range.( A1 ).PasteSpecial d.从文件尾部开始粘贴: oExcel.ActiveSheet.Range.PasteSpecial 16) 插入一行或一列: a. oExcel.ActiveSheet.Rows(2).Insert b. oExcel.ActiveSheet.Columns(1).Insert 17) 删除一行或一列: a. oExcel.ActiveSheet.Rows(2).Delete b. oExcel.ActiveSheet.Columns(1).Delete 18) 打印预览工作表: oExcel.ActiveSheet.PrintPreview 19) 打印输出工作表: oExcel.ActiveSheet.PrintOut 20) 工作表保存: if not oExcel.ActiveWorkBook.Saved then oExcel.ActiveSheet.PrintPreview 21) 工作表另存为: oExcel.SaveAs( C:ExcelDemo1.xls ) 22) 放弃存盘: oExcel.ActiveWorkBook.Saved = True 23) 关闭工作簿: oExcel.WorkBooks.Close 24) 退出 Excel: oExcel.Quit (二) 使用VBS 控制Excle二维图 1)选择当第一个工作薄第一个工作表 set oSheet=oExcel.Workbooks(1).Worksheets(1) 2)增加一个二维图 achart=oSheet.chartobjects.add(100,100,200,200) 3)选择二维图的形态 achart.chart.charttype=4 4)给二维图赋值 set series=achart.chart.seriescollection range=sheet1!r2c3:r3c9 series.add range,true 5)加上二维图的标题 achart.Chart.HasTitle=True achart.Chart.ChartTitle.Characters.Text= Excle二维图 6)改变二维图的标题字体大小 achart.Chart.ChartTitle.Font.size=18 7)给二维图加下标说明 achart.Chart.Axes(xlCategory, xlPrimary).HasTitle = True achart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = 下标说明 8)给二维图加左标说明 achart.Chart.Axes(xlValue, xlPrimary).HasTitle = True achart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = 左标说明 9)给二维图加右标说明 achart.Chart.Axes(xlValue, xlSecondary).HasTitle = True achart.Chart.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = 右标说明 10)改变二维图的显示区大小 achart.Chart.PlotArea.Left = 5 achart.Chart.PlotArea.Width = 223 achart.Chart.PlotArea.Height = 108 如何用vbs把excel的单元格数据写到txt 复制代码 代码如下: If WScript.Arguments.Count 0 Then Filename = WScript.Arguments(0) Set a = CreateObject(Excel.Application) If Filename = Then Filename = a.GetOpenFilename(Excel Files (*.xls), *.xls) If VarType(Filename) = vbBoolean Then MsgBox Excel2Txt用于将Excel文件的每个Sheet保存为一个文本文件。 & vbCr & vbLf & vbCr & vbLf & 用法: Excel2Txt filename.xls 或在对话框中打开Excel文件。 WScript.Quit End If End If Set w = a.Workbooks.Open(Filename) n = Replace(Replace(w.Name, .xls, ), .XLS, ) a.DisplayAlerts = False For Each s In w.Sheets s.SaveAs w.Path & & n & _ & s.Name & .txt, 20 Next a.Quit 把以上代码存为Excel2Txt.vbs双击执行就行了 VBS操作Excel 复制代码 代码如下: Set objExcel = CreateObject(Excel.Application) 建一个exel对象 Set objWorkbook = objExcel.Workbooks.Open _ (E:DOCHewl领域模型.xls) 打开文件 strToBeWrited = - & vbcrlf & _ - Generated by ScriptGenerator - & vbcrlf & _ - & vbcrlf & vbcrlf Count = objWorkbook.WorkSheets.Count 取sheet数量 Set my = CreateObject(Excel.Sheet) 新建sheet对象 For Each my In objWorkbook.WorkSheets 遍历sheet If my.Name = 目录 or my.Name = SecondHandHouse Then do nothing Else Wscript.Echo my.Name 获得sheet名字 Wscript.Echo my.Rows.Count strToBeWrited = strToBeWrited & create table & my.Name & vbcrlf strToBeWrited = strToBeWrited & /*=*/ & vbcrlf strToBeWrited = strToBeWrited & /* Table: & my.Name & */ & vbcrlf strToBeWrited = strToBeWrited & /*=*/ & vbcrlf strToBeWrited = strToBeWrited & create table & my.Name & ( & vbcrlf rowNum = 3 Do 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 null If not my.Cells(rowNum,9).Value = Then strToBeWrited = strToBeWrited & default & my.Cells(rowNum,9).Value End If strToBeWrited = strToBeWrited & , & vbcrlf rowNum = rowNum + 1 Loop strToBeWrited = strToBeWrited & constraint PK_ & my.Name & primary key (id) & vbcrlf strToBeWrited = strToBeWrited & ) & vbcrlf End If strToBeWrited = strToBeWrited & vbcrlf Next For Each my In objWorkbook.WorkSheets 遍历sheet If my.Name = 目录 or my.Name = SecondHandHouse Then do nothing Else strToBeWrited = strToBeWrited & constraint PK_ & my.Name & primary key (id) & vbcrlf strToBeWrited = strToBeWrited & ) & vbcrlf End If strToBeWrited = strToBeWrited & vbcrlf Next 写文件 set fs =createobject(scripting.filesystemobject) set f = fs.opentextfile(E:DOCHewldbscript.sql,2, true) Wscript.Echo strToBeWrited f.write strToBeWrited f.close Set f = nothing Set fs = nothing objExcel.Quit 结束退出 复制代码 代码如下: Dim Excel Set Excel = CreateObject(Excel.Application) 不显示提示信息,这样保存的时候就不会提示是否要覆盖原文件 Excel.DisplayAlerts=FALSE 调用EXCEL文件的时候不显示 Excel.visible=FALSE Excel.workbooks.open(D:test.XLS) 将sheet1设置为活动sheet Excel.workbooks(1).activate 插入行,这条我找MSDN都没找到,最后乱试试出来的 Excel.ActiveSheet.rows(1).insert Excel.ActiveSheet.Cells(1,1).Value = Date Excel.ActiveSheet.Cells(1,2).Value = row1 Excel.ActiveSheet.Cells(1,3).Value = comment1 Excel.ActiveSheet.rows(2).insert Excel.ActiveSheet.Cells(2,1).Value = Date Excel.ActiveSheet.Cells(2,4).Value = row2 Excel.ActiveSheet.Cells(2,7).Value = comment2 Excel.save Excel.quit Set Excel = Nothing Excel.ActiveSheet.rows(1).insert 不用找MSDN,在EXCEL帮助中就能找到,看“编程信息”/“Microsoft Excel Visual Basic 参考”/“属性”/“Q-R”/“Rows 属性”的介绍,和“编程信息”/“Microsoft Excel Visual Basic 参考”/“方法”/“I-L”/“Insert 方法”的介绍,就能明白这条语句的语法。 因为在EXCEL的VBA中,“Rows”、“Columns”、“Cells”属性返回的都是Range对象,所以对它们的应用可以等同Range对象的应用。 例如:你在EXCEL的VBA编辑器中可以这样写 cells(1,1).value=abc cells(1,1).wraptext=false 在写这些语句时,你应该注意到,在写完“cells(1,1).”之后,并没有弹出应该弹出的属性/方法列表,但是这些语句确实可以正常运行。 我的方法是:在EXCEL中录制宏,然后在EXCEL的VBA编辑器中修改语句,调试运行无误后再粘贴到VBS语句中,进行适当的修改。一、声明变量Dim a as integer 声明a为整型变量Dim a 声明a为变体变量Dim a as string 声明a为字符串变量Dim a as currency ,b as currency ,c as currency 声明a,b,c为货币变量声明变量可以是:Byte、Boolean、Integer、Long、Currency、Single、Double、Decimal(当前不支持)、Date、String(只限变长字符串)、String * length(定长字符串)、Object、Variant、用户定义类型或对象类型。二、声明常数ConstConst My = 456 常数的默认状态是 Private。Public Const MyString = HELP 声明 Public 常数。Private Const MyInt As Integer = 5 声明 Private Integer 常数。Const MyStr = Hello, MyDouble As Double = 3.4567 在同一行里声明多个常数。三、行数列数查看行数:hang=sheet.UsedRange.Rows.CountEndRow = Sheets(sheetsCaption).Range(Col & 65536).End(xlUp).Row查看列数:lie=sheet.UsedRange.Columns.Count返回选定区域的行数:MsgBox Selection.Rows.Count返回选定区域的列数:MsgBox Selection.Columns.Count返回选定区域的地址:Selection.Address四、赋值:Workbooks(12.xls).Worksheets(sheet2).Range(B5).Value = abcdeSheets(sheet1).Range(a1).Value = hellosheets(sheet2).selectRange(a1).value=helloSheets(sheet1).Range(a1)selectActiveCell.Value = 你好! 五、工作表相关:ActiveSheet.Name = liu 给活动工作表改名liuWorksheets.Add 增加一个工作表activesheet.delete 删除活动工作表Workbooks.Open FileName:=C:My DocumentsBook2.xls 打开一个工作簿文件ActiveWindow.Close 关闭活动窗口With Sheets(sheet1) 设定操作区域End With示例: Application.ScreenUpdating = FalseDim sheetsCaption As String: sheetsCaption = Sheet4Dim EndRow As Integer: EndRow = Sheets(sheetsCaption).Range(B & 65536).End(xlUp).RowDim i As Integer: i = 1With Sheets(sheetsCaption)Doi = i + 1Range(d & i).Value = Range(b & i).Value & Range(c & i).ValueLoop While i EndRow + 1End WithMsgBox 完毕Application.ScreenUpdating = True六、行的操作:Rows(i & : & i).Select剪切I行到行上面Selection.CutRows(4:4).SelectSelection.Insert Shift:=xlDown七、单元格位移:sub my_offsetActiveCell.Offset(0, 1).Select当前单元格向左移动一格ActiveCell.Offset(0, -1).Select当前单元格向右移动一格ActiveCell.Offset(1 , 0).Select当前单元格向下移动一格ActiveCell.Offset(-1 , 0).Select当前单元格向上移动一格end sub八、单元格的设置:Range(A5).Select设置成红色With Selection.Interior.ColorIndex = 3九、报错:如果上述程序产生错误那是因为单元格不能移动,为了解除上述错误,我们可以往sub my_offset 之下加一段代码 on error resume next忽略所有的错误ON ERROR RESUME NEXT遇错跳转:on error goto err_handle中间的其他代码err_handle: 标签跳转后的代码十、单元格格式:ActiveCell.Clear 清除单元格:删除所有文字、批注、格式Selection.HorizontalAlignment = xlLeft 选定单元格左对齐Selection.HorizontalAlignment = xlCenter 选定单元格居中Selection.HorizontalAlignment = xlRight 选定单元格右对齐Selection.Style = Percent 选定单元格为百分号风格Selection.Font.Bold = True 选定单元格字体为粗体Selection.Font.Italic = True 选定单元格字体为斜体With Selection.Font 选定单元格字体为宋体20号字.Name = 宋体.Size = 20End Withmsgbox ActiveCell.Address 得到当前单元格的地址msgbox date & chr(13) & time 得到当前日期及时间十一、VBA专属特
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2030年中国净水桶行业市场发展监测及投资潜力预测报告
- 2025至2030年中国装配式建筑行业市场供需格局及行业前景展望报告
- 配电网规划建设标准及相关知识测试试卷
- 2024年江苏省盐城市社会工作者职业资格社会工作法规与政策模拟考试含答案
- 行政事务采购审批流程标准工具
- 2025年船用变压器行业当前竞争格局与未来发展趋势分析报告
- 2025年文化遗产保护与修复工程师综合能力考试试卷及答案
- 2025年文化创意产业政策实施考核试卷及答案
- 动员部署类排比句(2025年8月12日)
- 古蔺城西加油站加气项目环评报告
- 2025-2030中国家政服务从业人员培训体系与职业发展白皮书
- 2025年安全风险分级管控培训考试试题(附答案)
- 厂区用电安全管理制度
- 2025年消防员招录面试题库及答案
- 初中英语新人教版八年级上册全册单词(2025秋)
- 2025年广西中考道德与法治试题答案详解讲评课件
- 农贸市场食品安全监管与能力提升培训
- 成人重症患者人工气道湿化护理专家共识解析与临床应用
- 模具订单流程管理规范
- 残疾孩子开学活动方案
- 英语作文初中教学课件
评论
0/150
提交评论