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.

Thursday, May 3, 2012

Deleting Empty Rows:-

To delete empty rows in a selected range we can use the following macro. The macro here uses the For Next Loop. First the macro counts the rows in a selected range to determine the when the macro should stop. The For Next statement acts as a counter.
Sub DelEmptyRow()
Rng = Selection.Rows.Count
ActiveCell.Offset(0, 0).Select
Application.ScreenUpdating = False
For i = 1 To Rng
If ActiveCell.Value = "" Then    'You can replace "" with 0 to delete rows with 'the value zero
Selection.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Select
End If
Next i
Application.ScreenUpdating = True
End Sub
Current Cell Address:-

To get the current cell address (or current range) to perhaps incorporate into your formula, you could use the following code.




Sub MyAddress()

MsgBox ActiveCell.Address 'absolute cell reference with the pesky $ signs

MsgBox ActiveCell.Address(RowAbsolute:=False, columnAbsolute:=False) 'without the $ signs, you may choose to omit either one Row / Column absolute

End Sub