Articles | Photoshop blog | Photography blog | about me | e-mail

Helen Bradley - MS Office Tips, Tricks & Tutorials

Sunday, January 21, 2007

AutoFormat an Excel cell

This Excel 2003 macro formats a cell depending on its contents when you type something in it. If you type a number, or a formula that returns a number, it is formatted one way, if you type a date it is formatted another way and if you type a word it is formatted a different way.

The macro uses the OnEntry event which fires whenever something is entered into a cell. Attach the macro to an Auto_Open macro to ensure it is run whenever the workbook is opened.

To create this, choose Tools, Macro, Visual Basic Editor and, choose Insert, Module to add a module to the current worksheet then type the code into it.

Back in Excel choose Tools, Macro, Auto_open to run the macro the first time to test it. Provided you have Excel configured to run macros, it will run automatically every time you open the workbook in future.

Sub Auto_Open()
ActiveSheet.OnEntry = "formatCell"
End Sub

Sub formatCell()
If IsNumeric(ActiveCell) Then
ActiveCell.Font.Name = "Verdana"
ActiveCell.Font.Size = 12
ActiveCell.Font.ColorIndex = 46
ElseIf IsDate(ActiveCell) Then
ActiveCell.Font.Name = "Verdana"
ActiveCell.Font.Size = 10
ActiveCell.Font.ColorIndex = 50
Else
ActiveCell.Font.Name = "Times New Roman"
ActiveCell.Font.Size = 12
ActiveCell.Font.ColorIndex = 5
End If
End Sub

Sub Auto_Close()
ActiveSheet.OnEntry = ""
End Sub

Add to Technorati Favorites

2 Comments:

At August 7, 2007 at 1:39 AM , Anonymous Anonymous said...

I cannot get Auto_Open working inside excel 2003 - can you help?

parkin_m@hotmail.com

 
At August 10, 2007 at 7:50 PM , Blogger Jane said...

Sure, can do. Probably the thing you are doing which you shouldn't be doing is to put the Auto_Open macro in your Personal.xls file. The Auto_open macro should go in the workbook itself in a new module.

I copied and pasted the code from this post directly into a new module in a new workbook, saved it and closed it and opened it and it works just fine in Excel 2003.

If you continue to have problems, post with more details of what the issue is or email me the Excel file at helen@helenbradley.com

cheers

Helen

 

Post a Comment

Please feel free to add your comment here. Thank you!

Subscribe to Post Comments [Atom]

<< Home