你可以做一個碼殼發電機。加入微軟引用窗體2.0對象庫,然後:
Public Sub SubShell(SubName As String)
Dim s As String
Dim DataObj As New MSForms.DataObject
s = "Sub " & SubName & "()" & vbCrLf
s = s & String(4, " ") & "With Application" & vbCrLf
s = s & String(8, " ") & ".ScreenUpdating = False" & vbCrLf
s = s & String(8, " ") & ".DisplayStatusBar = False" & vbCrLf
s = s & String(4, " ") & "End With" & vbCrLf & vbCrLf
s = s & String(4, " ") & "'Insert Code" & vbCrLf & vbCrLf
s = s & String(4, " ") & "With Application" & vbCrLf
s = s & String(8, " ") & ".ScreenUpdating = True" & vbCrLf
s = s & String(8, " ") & ".DisplayStatusBar = True" & vbCrLf
s = s & String(4, " ") & "End With" & vbCrLf
s = s & "End Sub" & vbCrLf
DataObj.SetText s
DataObj.PutInClipboard
End Sub
如果你現在在即時窗口中鍵入SubShell "Test"
,您希望子定位光標,然後再粘貼,你會看到下面的出現:
Sub Test()
With Application
.ScreenUpdating = False
.DisplayStatusBar = False
End With
'Insert Code
With Application
.ScreenUpdating = True
.DisplayStatusBar = True
End With
End Sub
你需要xlsb文件,如果你想分享excel代碼或者vbscript也可以幫到你。 –
對不起?我不明白你的意思... – user1
這[示例](http://codereview.stackexchange.com/questions/117037/vba-class-to-persist-and-restore-excel-application-properties)使用類來堅持並恢復應用程序設置。或者以類似的方式另一種[方法](http://codereview.stackexchange.com/questions/142793/applicationsettings-class-for-disabling-restoring-application-state)。 – ThunderFrame