Free Excel Tutorial for Engineers : Macros

MS Excel as we know is the most popular spreadsheet. One of its successes was because of an excellent feature called macro. Macros are snippet of code that you can write in your excel sheet which in turn perform some tasks programmatically. But if you know a little bit of writing excel macros then you can add immense power to your excel worksheets. By writing excel macros you can build solutions for two problems. One is you can avoid repetitive tasks. That is if you have to perform the same procedure repeatedly for different sets of data you can create a macro for that.  The other is the situation where the excel functions are incapable of doing what you want. Then you have to consider writing excel macros as a solution.  Writing excel macros is done by using a tool called VBA (Visual Basic for Applications).

Following is a Macro solution for one of the common Excel problems. You may be aware that in Excel, decimal places of given number can be easily changed. If you have a cell value at 3.456 if you set the number of decimals to 2, then excel will show the cell as 3.46 whereas for all calculation purposes it will consider 3.456 only. To avoid such difference you may have to round the cell value by putting Round Function. Following Example is a simple set of code written to convert a number into 2 Decimal places, to avoid using actual values in any other function.

Option Explicit

Sub Round2()
    Dim curC As Range
    For Each curC In Selection
        curC.Value = Format$(curC.Text, "0.00")
    Next
End Sub