Sunday, March 25, 2012

Cleaning Macro Code

If you look closely to every auto generated VBA code, a lot of unnecessary code taking up space. Emitting them makes your code faster and cleaner. Plus, it is much more easier to understand. Well, I came from UNIX/LINUX background. Unix/Linux coders always want their code to be small, short, simple, and such. That's why Unix-like operating systems are always fast. ;-) Except AIX. It's an ancient unix. :-p



Example
Range("C3:C7").Select
Selecting.Copy
Range("V2").Select
Selection.PasteSpecial Paste := xlValues,


Truncate the above snippet to

Range("C3:C7").Copy
Range("V2").PasteSpecial Paste := xlValues,


And for Font related code, Macro seems to generate unnecessary lines of code which are already consider default. VBA does not need to verify each of these default lines
Example

Range("F1").Select
With Selection.Font
.Name = "Tahoma"
.Size = 10
.Strikethrough = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic

Delete those unnecessary lines to only ...


Range("F1").Select
With Selection.Font
.Name = "Tahoma"
.Size = 10

No comments: