通過前面的學習,我們可以錄製宏,指定宏按鈕,啟用宏,也可以刪除宏。
不是所有的操作都可以錄製,有時候,發現錄製了一個空空如也。
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
宏不可以實現人機對話
比如電腦問你,你來回答,從而執行操作,如下典型的人機對話,錄製宏實現不了。
無法實現循環分支
比如從第一個文件一直到最後一個文件,或者是滿足某個條件執行某種操作,這些都無法通過錄製宏來實現。
缺少通用性,不夠靈活
假設錄製了一個宏,一鍵將這個月的員工信息,數據分析並生成圖表,下個月,公司又添加了幾個員工,再次運行可能出錯。
所以我們還是需要自己來編寫一些代碼。僅管如此,初學者可以大量利用錄製的宏,稍做些修改,然後就可以實現特殊的效果。
從下一文章,開始自己編寫一些簡單的代碼。
子曰:用之則行,不用則藏。
意思是說,如果你用我的這些建議,就馬上行動,知行合一,如果你不用,就趕緊收藏,以絕後患。
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺「網易號」用戶上傳並發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.