Solved

Simple macro writing to format MS Excel fields

Posted on 2007-03-23
2
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

688 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