?
Solved

Simple macro writing to format MS Excel fields

Posted on 2007-03-23
2
Medium Priority
?
265 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

777 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