add a zero if only one digit

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
mduffin
Newbie
Posts: 19
Joined: Fri Nov 12, 2010 4:16 pm

add a zero if only one digit

Post by mduffin » Mon Sep 23, 2013 7:49 pm

Can MS look at an excel cell and determine if the cell has a one digit number or a two digit number?
If it does have only one digit number, then add a zero?

So, if it's 1-9, then add a zero to the front of the number to make it 01-09?

And I'll have a column of numbers that will differ in amount each day.

So part two I guess is how to make it stop running when there's nothing in the cell?

Thanks in advance!

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Tue Sep 24, 2013 6:01 am

I didn't think excel supported 0 (zeros) at the start of a number. Eg if it was a CSV and put into excel it would show as 1 unless you change the format of the cell.
FIREFIGHTER

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Post by Grovkillen » Tue Sep 24, 2013 6:47 am

Cyber,

It's not entirely correct, but the only way to over ride this is to add a "'" before the numbers. I.e. "'01" this tells Excel to treat the number as a text. Please observe that Excel still is able to calculate the cell numbers as text:

Cell A1
"'02"

Cell A2
"'03"

Cell A3
"=A1*A2"
Would compute as "6"
Let>ME=%Script%

Running: 15.0.27
version history

mduffin
Newbie
Posts: 19
Joined: Fri Nov 12, 2010 4:16 pm

Post by mduffin » Tue Sep 24, 2013 11:07 am

Correct, either way an keep excel from removing the zero..
I've changed the formatting of the columns to text, and now need to add a zero to the single digit numbers.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Sep 24, 2013 12:10 pm

While Macro Scheduler could do this, it is a bit overkilll when Excel can do it for you using =Text(A1,"00")
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mduffin
Newbie
Posts: 19
Joined: Fri Nov 12, 2010 4:16 pm

Post by mduffin » Tue Sep 24, 2013 12:25 pm

Sorry, maybe I wasn't clear.
The excel sheet already exists (it's a report that gets run every day with very limited options) and it's filled with numbers..
So I switch the format of the columns to text and add a zero to any 1-9's because I need two digit numbers

I don't feel like going through a couple hundred numbers every day and adding a zero if the number is 1-9. So I'm hoping MS can do it for me.

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Post by Grovkillen » Tue Sep 24, 2013 12:45 pm

Okay, here's an example I just made.

Before running this make a XLS file named "TEST.XLS" and add numbers to the A coulumn (on sheet "Sheet1"!).
(I added 1-20 for cell A1-A20 and then 1-20 for cell A21-A40).
Save the file to your script dir.

Run this code (please save the script to the same location as "TEST.XLS"):

Code: Select all

XLSheetToArray>%SCRIPT_DIR%\TEST.XLS,Sheet1,SheetArray
XLOpen>%SCRIPT_DIR%\TEST.XLS,0,xlBook
Let>k=0
Repeat>k
  Let>k=k+1
    Let>CellToEvaluate=SheetArray_%k%_1
    If>CellToEvaluate<10
        Let>UpdateCellValue='0%CellToEvaluate%
    Else>
        Let>UpdateCellValue='%CellToEvaluate%
    Endif>
    XLSetCell>xlBook,Sheet1,%k%,1,%UpdateCellValue%,res
Until>k=SheetArray_count
XLSave>xlBook,%SCRIPT_DIR%\TEST.XLS
XLQuit>xlBook
You get the idea... just elaborate to fit your scenario.
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Sep 24, 2013 1:00 pm

You could do it like that but that seems like overkill. One simple Excel formula will do it too. Here's a video of me recording a macro to do it:

https://mjtnet.viewscreencasts.com/5e6f ... 62ed78c318

You could now just run this macro. The macro could always be in another book if you can't modify your source workbook. Macro Scheduler could run the macro if you need it to, or you could convert it to VBScript and run inside Macro Scheduler.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Post by Grovkillen » Tue Sep 24, 2013 1:03 pm

Marcus, most roads lead to Rome. :) Anyway, what's over kill and not I leave to the rest to judge.
Let>ME=%Script%

Running: 15.0.27
version history

mduffin
Newbie
Posts: 19
Joined: Fri Nov 12, 2010 4:16 pm

Post by mduffin » Tue Sep 24, 2013 1:20 pm

Thank you both! Marcus, I'm sorry I didn't understand that that little formula would do what I wanted the first time you posted it!

Thanks again!

mduffin
Newbie
Posts: 19
Joined: Fri Nov 12, 2010 4:16 pm

Post by mduffin » Tue Sep 24, 2013 1:31 pm

What's more.. if you do =TEXT(J9,"1900")
it turns a two digit year into a four digit year!!

Thanks a lot!

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Wed Sep 25, 2013 12:28 am

Grovkillen wrote:It's not entirely correct, but the only way to over ride this is to add a "'" before the numbers. I.e. "'01" this tells Excel to treat the number as a text.
Thanks for that I didn't know that, always learn something new on MJT Forums.
FIREFIGHTER

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts