Solved

Simple macro writing to format MS Excel fields

Posted on 2007-03-23
2
258 Views
Last Modified: 2012-05-05
I am new to macro writing and need some help.  I need to write a simple VB macro for MS Excel that will format two columns:

One column I need to add a 0 before the number.  So that when the user enters 7 in the Excel column the macro will format that column by adding a 0 in front of it. The result will be a 07 in the field.

The other column I need to have the macro convert a month and day date to this fomat MM/DD.

Some basic macro code I have is:

Range("I1").Select
    ActiveCell.FormulaR1C1 = "X"
    Range("I2").Select
    ActiveWorkbook.Save

but I need to format the cells.  how do I do this?
0
Comment
Question by:TimFred
2 Comments
 
LVL 13

Expert Comment

by:WJReid
Comment Utility
Hi,

Why don't you use the Formatting that is already there for dates and Numbers, from the Format Cells|Numbers tab?

If you select Custom then in the Type, before General, type "0", so you have "0"General
You can set the date format from there as well

Bill

0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
Comment Utility
First of all, try and avoid selecting cells. You rarely need to do this. You can also refer to more than one cell at once.

    [I1:I10].Formula = "=A1"

To set the formatting to use a leading zero:

    [I1:I10].NumberFormat = "00"

To set the date format:

    [J1:J10].NumberFormat = "MM/DD"

Kevin
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now