Excel
Visual Basic for Applications(VBA)是Visual Basic的宏语言,主要可用于扩展windows APP功能,尤其是Microsoft Office软件。 也可以说是APP视觉化的Basic Script。 1994年推出的excel 5.0版具有VBA的宏功能。
实用的EXCEL VBA编程小结:
最近工作场所内的项目用于生成报表EXCEL。 JAVA的poi虽说有这个能力,但可能还是很麻烦,所以转用. net,将visual studio 2003和office 2003组合使用了几个VBA。 因此,总结起来,选择了一些资料进行总结
首先创建Excel对象,然后使用ComObj :
Dim ExcelID as Excel.Application
setexcelidasnewexcel.application
1、显示当前窗口:
ExcelID.Visible :=True;
2、更改Excel标题栏:
ExcelID.Caption :=&; #039; APP应用程序为Microsoft Excel&; #039;
3、添加新工作簿:
ExcelID.WorkBooks.Add;
4、打开现有工作簿:
excel id.workbooks.open ( c:\ excel\demo.xls );
5、将第二个工作表设置为活动工作表:
ExcelID.WorkSheets[2].Activate;
或excelid.worksheets[&; #039; 工作表2 & amp; #039; ].activate;
6、为单元格赋值:
excel id.cells [ 1,4 ].value:=& amp; #039; 第一行第四列&; #039;
7、设置指定列宽(单位) (字符数),以第一列为例。
excel id.active sheet.columns [1].columns width:=5;
8、设置指定行的高度(单位:磅) ( 1磅=0.035 ),作为第二行为示例:
excel id.active sheet.rows [2].rowheight:=1/0.035; //1厘米
第9、8行前插入分页符:
excel id.worksheets [1].rows [8].page break:=1;
十、在第8栏前删除分页符:
excel id.active sheet.columns [4].page break:=0;
11、指定边框线宽:
excelid.activesheet.range[&; #039; B3:D4&; #039; ].Borders[2].weight:=3;
1-左2-右3-顶4-底5-倾斜( (6-倾斜(/) ) ) ) ) ) ) ) )。
12、清除第1行第4列单元格公式:
excel id.active sheet.cells [ 1,4 ].clear contents;
13、设置第一行字体属性:
excel id.active sheet.rows [1].font.name:=& amp; #039; 奴隶书&; #039;
excel id.active sheet.rows [1].font.color:=clblue;
excel id.active sheet.rows [1].font.bold:=true;
excel id.active sheet.rows [1].font.underline:=true;
14、进行页面设置:
a .页眉:
excel id.active sheet.page setup.center header:=& amp; #039; 报告演示文稿&; #039;
b .页脚:
excel id.active sheet.page setup.center footer:=& amp; #039; 第p页&; #039;
c .表头至上端边距2cm :
excel id.active sheet.page setup.header margin:=2/0.035;
d .页脚下边界3cm :
excel id.active sheet.page setup.header margin:=3/0.035;
e .上边距2cm :
excel id.active sheet.page setup.top margin:=2/0.035;
f .底边距离2cm :
excel id.active sheet.page setup.bottom margin:=2/0.035;
g .左起2厘米:
excel id.active sheet.page setup.left margin:=2/0.035;
h .右间距2cm :
excel id.active sheet.page setup.right margin:=2/0.035;
I .页面水平居中:
excel id.active sheet.page setup.center horizontally:=2/0.035;
j .页面垂直居中:
excel id.active sheet.page setup.center vertically:=2/0.035;
k .打印单元格网线:
excel id.active sheet.page setup.print gridlines:=true;
15、复印操作:
a .复制整个工作表:
excel id.active sheet.used.range.copy;
b .复制指定区域:
excelid.activesheet.range[&; #039; a1:E2&; #039; ] .复印;
c .从a1职位贴上:
excel id.active sheet.range.[ & amp; #039; a1&; #039; ].pastespecial;
d .从文件末尾粘贴:
excel id.active sheet.range.pastespecial;
16 .插入一行或一列:
a.excel id.active sheet.rows [2].insert;
b.excel id.active sheet.columns [1].insert;
17、删去第一行或第一列:
a.excel id.active sheet.rows [2].delete;
b.excel id.active sheet.columns [1].delete;
18、打印预览工作表:
excel id.active sheet.print preview;
19、打印输出工作表:
ExcelID.ActiveSheet.PrintOut;
20、保存工作表:
ifnotexcelid.active workbook.saved then
excel id.active sheet.print preview
End if
21、工作表另存为:
excelid.saveas(c:(excel ) demo1.xls );
22、放弃存储:
excel id.active workbook.saved:=true;
23、关闭工作簿:
ExcelID.WorkBooks.Close;
24、退出Excel :
ExcelID.Quit;
25、设置工作表密码:
excel id.active sheet.protect & amp; #039; 123&; #039;DrawingObjects:=True,Contents:=True,Scenarios:=True
26、EXCEL显示方式最大化
excel id.application.window state=XL maximized
27、工作簿显示方式最大化
excel id.active window.window state=XL maximized
28、设置打开默认工作簿的数量
ExcelID.SheetsInNewWorkbook=3
29、关闭时是否提示保存(保存为真; 不保存假)
ExcelID.DisplayAlerts=False
30、设置拆分窗口,固定行位
excel id.active window.split row=1
excel id.active window.freeze panes=true
31、设置打印时固定打印内容
excel id.active sheet.page setup.printtitlerows=& amp; #039; $1:$1&; #039;
32、设置打印标题
excel id.active sheet.page setup.printtitlecolumns=& amp; #039; &; #039;
33、设置显示方式(寻呼方式的显示)
excel id.active window.view=xlpagebreakpreview
34、设定显示比例
ExcelID.ActiveWindow.Zoom=100
35、让Excel响应DDE请求
ex.application.ignoreremoterequests=false
在VB中操作EXCEL
Private Sub Command3_Click (
On Error GoTo err1
Dim i As Long
Dim j As Long
dim obj exl as excel.application & amp; #039; 声明对象变量
Me.MousePointer=11&; #039; 改变鼠标样式
set obj exl=new excel.application & amp; #039; 初始化对象变量
obj exl.sheetsinnewworkbook=1& amp; #039; 将新工作簿的数量设置为1
objExl.Workbooks.Add&; #039; 将工作簿添加到
obj exl.sheets ( obj exl.sheets.count ).Name=&; #039; 图书1 & amp; #039; &; #039; 重命名工作簿
objExl.Sheets.Add,objexl.sheets(&; #039; 图书1 & amp; #039; )“在第一个之后做第二个工作”
obj exl.sheets ( obj exl.sheets.count ).Name=&; #039; 图书2 & amp; #039;
objExl.Sheets.Add,objexl.sheets(&; #039; 图书2 & amp; #039; )“增加第三份工作,在第二份之后淡化”
obj exl.sheets ( obj exl.sheets.count ).Name=&; #039; 图书3 & amp; #039;
objexl.sheets(book1 ) ).Select选择工作簿
For i=1 To 50&; #039; 循环写入数据
For j=1 To 5
If i=1 Then
obj exl.selection.numberformatlocal=& amp; #039; @&; #039; &; #039; 将被格式化为文本
objexl.cells(I,j )=&; #039; E &; #039; i j
Else
objexl.cells(I,j )=i j
End If
下一步
下一步
objexl.rows(1:1) ).Select )选中第一行
obj exl.selection.font.bold=true & amp; #039; 把字加粗
obj exl.selection.font.size=24 & amp; #039; 设定字体大小
obj exl.cells.entire column.autofit & amp; #039; 自动调整列宽
obj exl.active window.split row=1& amp; #039; 在中分割第一行
obj exl.active window.split column=0& amp; #039; 按列分割列
obj exl.active window.freeze panes=true & amp; #039; 固定分割obj exl.active sheet.page setup.printtitlerows=& amp; #039; $1:$1&; #039; &; #039; 打印固定行的设定
obj exl.active sheet.page setup.printtitlecolumns=& amp; #039; &; #039; &; #039; 打印标题obj exl.active sheet.page setup.right footer=& amp; #039; 打印时间: &; #039; _}
format(now,&; #039; yyyy年mm月dd日hh:MM:ss&; #039; ) ) )。
obj exl.active window.view=xlpagebreakpreview & amp; #039; 设定显示方法
obj exl.active window.zoom=100 & amp; #039; 设定显示尺寸
在工作表中添加密码
objExl.ActiveSheet.Protect &; #039; 123&; #039;DrawingObjects:=True,_
Contents:=True,Scenarios:=True
obj exl.application.ignoreremoterequests=false
objExl.Visible=True&; #039; 使EXCEL可见
obj exl.application.window state=XL maximized & amp; #039; excel的显示方式最大化
obj exl.active window.window state=XL maximized & amp; #039; 最大化工作簿显示
obj exl.sheetsinnewworkbook=3& amp; #039; 将默认的新工作簿数量更改为三个
Set objExl=Nothing&; #039; 清除对象
Me.MousePointer=0&; #039; 修改鼠标
Exit Sub
err1:
objExl.SheetsInNewWorkbook=3
objExl.DisplayAlerts=False&; #039; 关闭时不要求保存
objexl.quit&; #039; 关闭excel
objExl.DisplayAlerts=True&; #039; 关闭时提示保存
Set objExl=Nothing
Me.MousePointer=0
结束子
小编寄言:
visualbasicforapplications ( VBA )是一种APP应用程序,用于设置使用VBA的工作表APP的管理权限、在EXCEL中提高VBA的效率以及在EXCEL中应用计时器。 有很多功能的使用方法,但编辑不一一介绍。 用户可以直接体验哦。