Excel VBA编程实例.docx

上传人:b****6 文档编号:11916473 上传时间:2023-06-03 格式:DOCX 页数:25 大小:17.23KB
下载 相关 举报
Excel VBA编程实例.docx_第1页
第1页 / 共25页
Excel VBA编程实例.docx_第2页
第2页 / 共25页
Excel VBA编程实例.docx_第3页
第3页 / 共25页
Excel VBA编程实例.docx_第4页
第4页 / 共25页
Excel VBA编程实例.docx_第5页
第5页 / 共25页
Excel VBA编程实例.docx_第6页
第6页 / 共25页
Excel VBA编程实例.docx_第7页
第7页 / 共25页
Excel VBA编程实例.docx_第8页
第8页 / 共25页
Excel VBA编程实例.docx_第9页
第9页 / 共25页
Excel VBA编程实例.docx_第10页
第10页 / 共25页
Excel VBA编程实例.docx_第11页
第11页 / 共25页
Excel VBA编程实例.docx_第12页
第12页 / 共25页
Excel VBA编程实例.docx_第13页
第13页 / 共25页
Excel VBA编程实例.docx_第14页
第14页 / 共25页
Excel VBA编程实例.docx_第15页
第15页 / 共25页
Excel VBA编程实例.docx_第16页
第16页 / 共25页
Excel VBA编程实例.docx_第17页
第17页 / 共25页
Excel VBA编程实例.docx_第18页
第18页 / 共25页
Excel VBA编程实例.docx_第19页
第19页 / 共25页
Excel VBA编程实例.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Excel VBA编程实例.docx

《Excel VBA编程实例.docx》由会员分享,可在线阅读,更多相关《Excel VBA编程实例.docx(25页珍藏版)》请在冰点文库上搜索。

Excel VBA编程实例.docx

ExcelVBA编程实例

ExcelVBA编程实例

Subdirect_Price()

''定义变量

DimcRowsAsInteger'总行数

DimcColumnsAsInteger'总列数

DimHEADERCOLORINDEXAsInteger'表头的背景色DimcTempAsInteger'临时计数

DimsTempStringAsString'临时字符串变量

DimiAsInteger'临时计数

DimjAsInteger'临时计数

DimrowIndexAsInteger'临时指示处理到哪里DimcolIndexAsInteger'临时指示处理到哪里DimtempRndColorAsInteger'临时生成的颜色DimTABLENAMEAsString'待处理的表名

DimcolorIndexAsString'颜色索引名字

'表头的背景色

HEADERCOLORINDEX=15

colorIndex=36'颜色从33开始是比较浅的颜色TABLENAME="direct_Price"'关闭所有弹出的警告消息

Application.DisplayAlerts=False'设置需要处理的单元表

Sheets(TABLENAME).Select

'取单元表的总列数与总行数

cRows=Sheets(TABLENAME).UsedRange.Rows.Count

cColumns=Sheets(TABLENAME).UsedRange.Columns.Count

''''

'选择所有的单元格

Range(Cells(1,1),Cells(cRows,cColumns)).Select

'设置该表中所有单元行高为11.25

Selection.RowHeight=11.25

'设置该表中所有单元行高为11.25

Selection.RowHeight=11.25

'设置所有的边框

Selection.Borders(xlDiagonalDown).LineStyle=xlNone

Selection.Borders(xlDiagonalUp).LineStyle=xlNone

WithSelection.Borders(xlEdgeLeft)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlEdgeTop)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlEdgeBottom)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlEdgeRight)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlInsideVertical)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

'并且拆分所有的单元格

WithSelection

.MergeCells=False'拆分单格

EndWith

Columns("C:

C").Select

Selection.InsertShift:

=xlToRight

'删除第一列,注意这里必须先拆分单格,再删除第一列,否则一次就会把合并单元格所在列全部删除

Range(Cells(1,1),Cells(1,1)).Select

Selection.EntireColumn.Delete

'Selection.EntireColumn.Delete

'向表头添加一行

Rows("1:

1").Select

Selection.Insert

Columns("A:

A").Select

Selection.ColumnWidth=9.29Columns("B:

B").Select

Selection.ColumnWidth=6.71Columns("C:

C").Select

Selection.ColumnWidth=15.29Columns("D:

D").Select

Selection.ColumnWidth=29.86Columns("E:

E").Select

Selection.ColumnWidth=12.29Columns("F:

F").Select

Selection.ColumnWidth=12.29

'''''设定单元格A1:

A2''

'合并A1:

A2单元格

Range("A1:

A2").Select

'将数据写回

WithSelection

.HorizontalAlignment=xlCenter

.VerticalAlignment=xlCenter

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

.MergeCells=True

EndWith

'往该单元格中写入Usage_Var

ActiveCell.FormulaR1C1="Price"

'设置该单元格字体格式

WithActiveCell.Characters(Start:

=1,Length:

=5).Font

.Name="Arial"

.FontStyle="加粗倾斜"

.Size=10

.Strikethrough=False

.Superscript=False

.Subscript=False

.OutlineFont=False

.Shadow=False

.Underline=xlUnderlineStyleNone

.colorIndex=2

EndWith

'单元格设定边框

Selection.Borders(xlDiagonalDown).LineStyle=xlNone

Selection.Borders(xlDiagonalUp).LineStyle=xlNone

Selection.Borders(xlEdgeTop).LineStyle=xlNone

WithSelection.Borders(xlEdgeBottom)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=56

EndWith

Selection.Borders(xlInsideHorizontal).LineStyle=xlNone

WithSelection.Interior

.colorIndex=5

.Pattern=xlSolid

.PatternColorIndex=xlAutomatic

EndWith

'''''设定头两行的内部样式'''''

Range("B1:

B2").Select

Selection.Merge

Range("C1:

C2").Select

Selection.Merge

Range("D1:

D2").Select

Selection.Merge

Range("B1:

D2").Select

'设置头两行行高为11.25

Selection.RowHeight=14.25

WithSelection.Font

.Name="Arial"

.FontStyle="加粗"

.Size=8

.Strikethrough=False

.Superscript=False

.Subscript=False

.OutlineFont=False

.Shadow=False

.Underline=xlUnderlineStyleNone

.colorIndex=xlAutomatic

EndWith

WithSelection

.HorizontalAlignment=xlCenter

.VerticalAlignment=xlCenter

.WrapText=True

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

EndWith

WithSelection.Interior

.colorIndex=HEADERCOLORINDEX

.Pattern=xlSolid

.PatternColorIndex=xlAutomatic

EndWith

Range("B1:

B2").Select

ActiveCell.FormulaR1C1="Type"

WithActiveCell.Characters(Start:

=1,Length:

=4).Font

.Name="Arial"

.FontStyle="加粗"

.Size=8

.Strikethrough=False

.Superscript=False

.Subscript=False

.OutlineFont=False

.Shadow=False

.Underline=xlUnderlineStyleNone

.colorIndex=5

EndWith

Range("E1:

F1").Select

WithSelection.Font

.Name="Arial"

.FontStyle="加粗"

.Size=8

.Strikethrough=False

.Superscript=False

.Subscript=False

.OutlineFont=False

.Shadow=False

.Underline=xlUnderlineStyleNone

.colorIndex=5

EndWith

WithSelection

.HorizontalAlignment=xlCenter

.VerticalAlignment=xlCenter

.WrapText=True

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

.MergeCells=True

EndWith

WithSelection.Interior

.colorIndex=HEADERCOLORINDEX

.Pattern=xlSolid

.PatternColorIndex=xlAutomatic

EndWith

ActiveCell.FormulaR1C1="Price"

Range("E2:

F2").Select

'设置头两行行高为11.25

Selection.RowHeight=14.25

WithSelection.Font

.Name="Arial"

.FontStyle="加粗"

.Size=8

.Strikethrough=False

.Superscript=False

.Subscript=False

.OutlineFont=False

.Shadow=False

.Underline=xlUnderlineStyleNone

.colorIndex=xlAutomatic

EndWith

WithSelection

.HorizontalAlignment=xlCenter

.VerticalAlignment=xlCenter

.WrapText=True

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

.MergeCells=False

EndWith

WithSelection.Interior

.colorIndex=HEADERCOLORINDEX

.Pattern=xlSolid

.PatternColorIndex=xlAutomatic

EndWith

'加第一二行边框

Range("A1:

F2").Select

Selection.Borders(xlDiagonalDown).LineStyle=xlNone

Selection.Borders(xlDiagonalUp).LineStyle=xlNone

WithSelection.Borders(xlEdgeLeft)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlEdgeTop)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlEdgeBottom)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlEdgeRight)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlInsideVertical)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

WithSelection.Borders(xlInsideHorizontal)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=xlAutomatic

EndWith

'去掉第三行的:

'sTempString=Right(Cells(3,1),Len(Cells(3,1))-3)

'ActiveCell.FormulaR1C1=sTempString

i=2

j=1

'外层循环判断是否都合并完成,这里插入了一行,加1

Whilei<=cRows

'i=i+1

Range(Cells(i+1,j),Cells(i+1,j)).Select

'去掉分类行中的:

If(Len(Cells(i+1,j))>=3)Then

''如果是分格的界限

If(Left(Cells(i+1,j),3)=":

")Then

Range(Cells(i+1,j),Cells(i+1,cColumns)).Select

'对第三行进行设定

'设置头两行行高为11.25

Selection.RowHeight=18

WithSelection.Interior

.colorIndex=2

.Pattern=xlSolid

.PatternColorIndex=xlAutomatic

EndWith

'合并前两格

'先将其合并

WithSelection

.HorizontalAlignment=xlLeft'靠左对齐

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

.MergeCells=False

EndWith

'合并

Selection.Merge

'对其设定字体风格

WithSelection.Font

.Name="Arial"

.FontStyle="加粗倾斜"

.Size=9

.Strikethrough=False

.Superscript=False

.Subscript=False

.OutlineFont=False

.Shadow=False

.Underline=xlUnderlineStyleNone

.colorIndex=3

EndWith

WithSelection

.HorizontalAlignment=xlLeft

.VerticalAlignment=xlCenter

.WrapText=True

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

.MergeCells=True

EndWith

sTempString=Right(Cells(i+1,j),Len(Cells(i+1,j))-3)

ActiveCell.FormulaR1C1=sTempString

i=i+1

EndIf

EndIf

i=i+1

'加1后判断是否到了表尾,没有继续合并处理

'If(i<=cRows+1)Then

rowIndex=i

'取出Cells(i,j)的内容

sTempString=Cells(i,j)

'循环判断下一个单元格是否和上一个单元格相等,不是则表示到此该合并

WhilesTempString=Cells(i+1,j)Andi<=cRows

i=i+1

Wend

设置第一列''''

'跳出循环表示已经到此该将rowIndex和i行合并

Range(Cells(rowIndex,j),Cells(i,j)).Select

Selection.Merge

'将原来内容填充进来

ActiveCell.FormulaR1C1=sTempString

'设合并后的单元格的边框

WithSelection

.HorizontalAlignment=xlCenter

.VerticalAlignment=xlCenter

.WrapText=True

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

.MergeCells=True

EndWith

Selection.Font.FontStyle="加粗"

设置第一列结束''''

'''设置第二列'''

Range(Cells(rowIndex,j+1),Cells(i,j+1)).Select

'设置字体

WithSelection.Font

.Name="Arial"

.FontStyle="加粗"

.Size=8

.Strikethrough=False

.Superscript=False

.Subscript=False

.OutlineFont=False

.Shadow=False

.Underline=xlUnderlineStyleNone

.colorIndex=5

EndWith

WithSelection

.HorizontalAlignment=xlCenter

.VerticalAlignment=xlCenter

.WrapText=True

.Orientation=0

.AddIndent=False

.IndentLevel=0

.ShrinkToFit=False

.ReadingOrder=xlContext

.MergeCells=False

EndWith

Selection.Borders(xlDiagonalDown).LineStyle=xlNone

Selection.Borders(xlDiagonalUp).LineStyle=xlNone

WithSelection.Borders(xlEdgeLeft)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=56

EndWith

WithSelection.Borders(xlEdgeTop)

.LineStyle=xlContinuous

.Weight=xlThin

.colorIndex=56

EndWith

WithSelection.Borders(xlEdgeBottom)

.LineStyle=xlContinuous

.Weight=xlTh

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

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2