Friday, June 8, 2012

Current Cell Position:-

Sometimes we need to know the current cell position. This would do the trick.

Sub MyPosition()
       myRow = ActiveCell.Row
       myCol = ActiveCell.Column
       Msgbox myRow & "," & myCol
End Sub
Error Trapping:-

Trapping errors are important as users can do marvelous things to mess up you macros. Here you can use either of these 2 statements.
         - On Error Resume Next   OR
         - On Error Goto ErrorTrap1
            ... more lines of code
            ErrorTrap1:
            ... more code (what to do if there is an error)
The first statement will allow the macro to continue the next line of code upon hitting an error but the second statement will run an alternative code should there be an error.
Errors in macros:-

Ever had a macro running perfectly one day and the next day errors keep on popping up even though you never made changes to that macro? This is no fault of yours. Due to the excel VBA design, macro files get badly fragmented due to heavy editing of macros, insertion of modules & userforms. What you need to do is copy your macros else where, delete the macros, save the file without macros. Open the file again and import the macros and save it once more with the macros. You macros will run properly until it gets fragmented again at a later stage. 
Excel Functions :-

Using Excel functions in VBA is almost the same as using them in a spreadsheet. For example to round an amount to 2 decimal places in a spreadsheet would be

=round(1.2345,2)

In VBA you would need to use the term Application followed by the function ie

ActiveCell = Application.round(ActiveCell, 2)
Flickering Screen:-
Sometimes when you run a macro, the screen flickers a lot due to the screen updating itself. This slows the macro done especially when the macro has a lot of work to do. You need to include the statement as shown below.

Also see Deleting Empty Rows

Application.ScreenUpdating = False

You need to set the screen updating back to true at the end of the macro.