1、开启Excel的开发工具,Microsoft Excel默认不开启此功能,需要手动开启。点击Excel左上角的【文件】,然后点击文件下方的【选项】
2、【Excel选项】中点击【自定义功能区】,右边勾选【开发工具】,确定
3、此时Excel工具栏出现【开发工具】模块,点击【开发工具】,我们将看到三个我们开发过程中必用的按钮,分别是:【Visual Basic】、【宏】、【录制宏】
4、点击【Visual Basic】,进入宏编辑环境,新建模块,根据需要命名,双击模块名字,右边就可以进行Excel VBA开发了
5、常用开发知识点选中单元格,我们经常会使用选中单元格精准操作:1、选中A1单元格 Range("A1").Select2、选中A1到B5区域 Range("A1:B5").Select
6、常用开发知识点单元格赋值:1、不选中单元格直接赋值 Range("A1").Value = "闭模高度"2、选中单元格赋值 Range("A1").Select ActiveCell.FormulaR1C1 = "600"注意:无法区域赋值,只能单个赋值
7、常用开发知识点,单元格颜色: '选中A1到B5区域 Range("A1:B5").Select '执行颜色命令,注意颜色是RGB值,什么是RGB自己百度查一下,最简单 'QQ截图上面就有显示的RGB值,具体看下面截图 With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 9119872 .TintAndShade = 0 .PatternTintAndShade = 0 End With
8、常用开发知识点,画表格: '选中A1到E5区域,外部粗线框,里面细线框 Ra荏鱿胫协nge("A1:E5").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone '整体表格左边线 With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With '整体表格上边线 With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With '整体表格下边线 With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With '整体表格右边线 With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With '整体表格内部横线竖线 With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With
9、常用开发知识点,单元格合并: Range("A1:A5").Select With Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With
10、常用开发知识点(重点),公式写入单元格: 'SUM是求和函数,和直接在单元格使用一样带括号 'RC是相对A5的相对位置,如下图横向为C,竖向为R,0点就是A5的位置 '箭头方向为正,尾巴方向为负 Range("A5").Select ActiveCell.FormulaR1C1 = "=SUM(R[-1]C:R[-4]C)"
11、巧用宏录制功能:首先知道自己在Excel上直接操作是怎么操作的,然后点击录制宏,把操作走遍,停止宏录制,点击宏就可以看到刚才录制的操作了,我们编辑这个宏就能看到VBA源代码怎么写的,将每一小步集合起来我们就能拼凑出一个很多操作的宏了,最后导出宏存起来,以下为操作截图