Microsoft Office Tutorials and References
In Depth Information
Editing a Macro with Comments and Improvements to the Code
Those two lines can and should be consolidated into one line, bypassing the selection activity:
Columns(“A”).Insert Shift:=xlToRight
Same with the next two statements…
Selection.Cut Destination:=Columns(“A:A”)
…which can be expressed more succinctly as
Columns(“C”).Cut Destination:=Columns(“A”)
You can see where I am going with this. In VBA, you can act directly upon most objects, most of the
time, without needing to select them. When you deleted column C, you never needed to touch it in
order for VBA to do the work for you because this…
Selection.Delete Shift:=xlToLeft
…can become this:
Columns(“C”).Delete Shift:=xlToLeft
Figure 4-4 shows how the original 13 lines of code in the mySort macro have been reduced to a
much more readable and highly efficient six lines. Also notice how comments can be added for the
purpose of enhancing the organized look of the macro. Your comments will help you, and anyone
reading the macro, to understand what the code lines are doing, and why they are doing it.
figurE 4-4
Search JabSto ::

Custom Search