用Excel中的宏获取VBA中的代码。

来源:互联网 发布:linux ftp命令大全 编辑:程序博客网 时间:2024/05/20 08:28

:>> 新建宏,ALT+F11,查看宏的原代码。

已单元格合并,自动换行,自动行高为例子:
工具 -> 宏 - > 录制新宏
操作Excel,设置表格属性,单元格合并,自动换行,最合适行高
停止宏录制

ALT + F11
VBAProject(Book1) -> 模块 -> 模块1
代码如下:
Sub Macro2()
'
' Macro2 Macro
' 宏由 abc 录制,时间: 2007-12-3
'
Columns("A:A").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True //自动换行
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True //合并单元格
End With
Selection.Rows.AutoFit //自适应行高
End Sub

在Delphi中的代码:
procedure MergeCells(AFirstRow, AFirstCol,
ALastRow, ALastCol: Integer; ASheet: OleVariant);
var
VFirstCell, VLastCell: OleVariant;
begin
VFirstCell := ASheet.Cells[AFirstRow, AFirstCol];
VLastCell := ASheet.Cells[ALastRow, ALastCol];
ASheet.Range[VFirstCell, VLastCell].Select;
ASheet.Application.Selection.WrapText := True;
ASheet.Application.Selection.Orientation := 0;
ASheet.Application.Selection.AddIndent := False;
ASheet.Application.Selection.ShrinkToFit := False;
ASheet.Application.Selection.MergeCells := True;
ASheet.Application.Selection.Rows.AutoFit;
end;

 
原创粉丝点击