Solved

Simple macro writing to format MS Excel fields

Posted on 2007-03-23
2
260 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

773 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