+ Reply to Thread
Results 1 to 6 of 6

Formatting cell based on cell contents

  1. #1
    Registered User
    Join Date
    02-04-2015
    Location
    Oregon, USA
    MS-Off Ver
    2010
    Posts
    3

    Formatting cell based on cell contents

    Dear all,
    I have looked for a solution on this and I can't seem to find one. My request is fairly simple but I am new to VBA programming and can't quite get my head around it.
    Basically I want to have a VBA code (cell or worksheet change event?) that operates in one column in a sheet e.g. C2:C1000 and keeps the number of characters that are entered in that cell to three, prefixing any value less than 3 characters with zero's. There will never be reason more than 3 numbers will be entered into this column.

    It doesn't matter to me (I dont think) if the resultant is a text string or numbers.

    For example.
    If I enter "3" into C2 I would like it to automatically change to "003"
    If I enter "32" into C2 I would like it to automatically change to "032"
    If "321" is entered I would like it to stay as "321"
    And then the same throughout the whole column down say 1000 rows or so.

    I could do it with formulas in a different cell but I want to keep the sheet streamlined.

    If it is possible I would be really appreciative of the help!

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    02-04-2015
    Location
    Brisbane, Australia
    MS-Off Ver
    2013
    Posts
    6

    Re: Formatting cell based on cell contents

    Hi msog81,

    There easiest way to add leading zeros to numbers is to convert it to text. Have a look at the code below:

    Please Login or Register  to view this content.
    This code will:
    - set the target cell to text
    - add zeros to the front of the number if required-
    - ignore the annoying number stored as text error

    To apply the solution to a range just run the following code:

    Please Login or Register  to view this content.
    And to automatically run the code when a cell in the range is changed, just add this code in the respective worksheet object in VB editor:

    Please Login or Register  to view this content.
    Let me know if that helped!

    Cheers,
    Mo

  3. #3
    Registered User
    Join Date
    02-04-2015
    Location
    Oregon, USA
    MS-Off Ver
    2010
    Posts
    3

    Re: Formatting cell based on cell contents

    Thanks for the quick reply MO!
    Could you give me some pointers on how you combined the codes to get it to run, every time I try I get either "run-time error '91': Object variable or With block variable not set" or "Run Time error '28': Out of Stack Space". Thanks again.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Formatting cell based on cell contents

    Try this file Sheet1 Column C and see if it works for you.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-04-2015
    Location
    Oregon, USA
    MS-Off Ver
    2010
    Posts
    3

    Re: Formatting cell based on cell contents

    Thanks John, that works PERFECTLY, thanks so much for taking the time to write to up for me.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Formatting cell based on cell contents

    You're welcome. Glad to help out and thanks for the feedback. Please mark this thread as solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 12-14-2013, 02:52 PM
  2. Replies: 1
    Last Post: 08-20-2012, 10:36 PM
  3. [SOLVED] Conditional Formatting based on contents in another cell
    By flisters in forum Excel General
    Replies: 3
    Last Post: 03-22-2012, 07:04 AM
  4. Replies: 6
    Last Post: 05-16-2008, 06:28 PM
  5. Macro to move cell contents based on formatting
    By Scott Wagner in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2005, 11:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1