首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

录制宏存在哪些致命的缺点

通过前面的学习,我们可以录制宏,指定宏按钮,启用宏,也可以删除宏。

不是所有的操作都可以录制,有时候,发现录制了一个空空如也。

1.录制宏会有大量多余的代码

如果录制一个宏,将选择的单元格,上下左右均加边框。天呀,居然有以下如此长的代码。

Sub Macro1()

Selection.Borders(xlDiagonalDown).LineStyle = xlNone

Selection.Borders(xlDiagonalUp).LineStyle = xlNone

With Selection.Borders(xlEdgeLeft)

.LineStyle = xlContinuous

.Weight = xlThin

.ColorIndex = xlAutomatic

End With

With Selection.Borders(xlEdgeTop)

.LineStyle = xlContinuous

.Weight = xlThin

.ColorIndex = xlAutomatic

End With

With Selection.Borders(xlEdgeBottom)

.LineStyle = xlContinuous

.Weight = xlThin

.ColorIndex = xlAutomatic

End With

With Selection.Borders(xlEdgeRight)

.LineStyle = xlContinuous

.Weight = xlThin

.ColorIndex = xlAutomatic

End With

With Selection.Borders(xlInsideVertical)

.LineStyle = xlContinuous

.Weight = xlThin

.ColorIndex = xlAutomatic

End With

With Selection.Borders(xlInsideHorizontal)

.LineStyle = xlContinuous

.Weight = xlThin

.ColorIndex = xlAutomatic

End With

End Sub

这段代码如果自己来编写,简短了好多

Sub Macro3()

With Selection

.Borders(xlEdgeLeft).LineStyle = xlContinuous

.Borders(xlEdgeTop).LineStyle = xlContinuous

.Borders(xlEdgeBottom).LineStyle = xlContinuous

.Borders(xlEdgeRight).LineStyle = xlContinuous

.Borders(xlInsideVertical).LineStyle = xlContinuous

.Borders(xlInsideHorizontal).LineStyle = xlContinuous

End With

End Sub

宏不可以实现人机对话

比如电脑问你,你来回答,从而执行操作,如下典型的人机对话,录制宏实现不了。

无法实现循环分支

比如从第一个文件一直到最后一个文件,或者是满足某个条件执行某种操作,这些都无法通过录制宏来实现。

缺少通用性,不够灵活

假设录制了一个宏,一键将这个月的员工信息,数据分析并生成图表,下个月,公司又添加了几个员工,再次运行可能出错。

所以我们还是需要自己来编写一些代码。仅管如此,初学者可以大量利用录制的宏,稍做些修改,然后就可以实现特殊的效果。

从下一文章,开始自己编写一些简单的代码。

子曰:用之则行,不用则藏。

意思是说,如果你用我的这些建议,就马上行动,知行合一,如果你不用,就赶紧收藏,以绝后患。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20201122A021KB00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券