Solved

Simple macro writing to format MS Excel fields

Posted on 2007-03-23
2
259 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
ID: 18781099
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
ID: 18781151
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

863 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

22 Experts available now in Live!

Get 1:1 Help Now