add a zero if only one digit
Moderators: Dorian (MJT support), JRL
add a zero if only one digit
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!
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!
- CyberCitizen
- Automation Wizard
- Posts: 721
- Joined: Sun Jun 20, 2004 7:06 am
- Location: Adelaide, South Australia
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
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"
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"
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
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.
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.
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
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"):
You get the idea... just elaborate to fit your scenario.
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
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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.
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
- CyberCitizen
- Automation Wizard
- Posts: 721
- Joined: Sun Jun 20, 2004 7:06 am
- Location: Adelaide, South Australia