Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default split a number into its digits

Hi Guys! I need to split numbers into its digits which i will latter assign
to variables. For instance:
originalNumber = 12345

then
a = 1
b = 2
c = 3
d = 4
e = 5
I am building a macro, so I cannot use the "text to columns trick" with
fixed delimiters
Thanks a lot guys!!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default split a number into its digits

Have you tried with string formulas such as MID?
Using a For ... Next loop you could write a procedure that extracts at
each recursion one of the digits, and you could store them in an
array.

For example:

For i=1 to Len(number)
digit(i) = MID(number, i, 1)
Next i

Antonio.


On 15 Oct, 17:08, Paco wrote:
Hi Guys! I need to split numbers into its digits which i will latter assign
to variables. For instance:
originalNumber = 12345

then
a = 1
b = 2
c = 3
d = 4
e = 5
I am building a macro, so I cannot use the "text to columns trick" with
fixed delimiters
Thanks a lot guys!!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default split a number into its digits

On 15 Oct, 17:20, "a.riva@UCL" wrote:
Have you tried with string formulas such as MID?
Using a For ... Next loop you could write a procedure that extracts at
each recursion one of the digits, and you could store them in an
array.

For example:

For i=1 to Len(number)
digit(i) = MID(number, i, 1)
Next i

Antonio.


Ooops :-)
Of course you have to declare the array digit as an array of doubles
(or integers):
Dim digit() As Double

Then you have to re-declare it to change its dimensions according to
the length of the number oyu want to split:
ReDim digit(1 To Len(number))

And only now you can apply the For... Next loop.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default split a number into its digits

Hi Antonio! thank you so much for your help. I tried the MID function. It
works great! Is there a way to convert the resulkting number from string to
an integer?
that is, the mid function returns a "5", instead of a 5

"a.riva@UCL" wrote:

Have you tried with string formulas such as MID?
Using a For ... Next loop you could write a procedure that extracts at
each recursion one of the digits, and you could store them in an
array.

For example:

For i=1 to Len(number)
digit(i) = MID(number, i, 1)
Next i

Antonio.


On 15 Oct, 17:08, Paco wrote:
Hi Guys! I need to split numbers into its digits which i will latter assign
to variables. For instance:
originalNumber = 12345

then
a = 1
b = 2
c = 3
d = 4
e = 5
I am building a macro, so I cannot use the "text to columns trick" with
fixed delimiters
Thanks a lot guys!!




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default split a number into its digits

An other way could be, if your number has no decimals, to divide
recursively the number by 10, and then assign to an array variable the
difference multiplied by 10 of the divided number and its integer
part.

An example, that's clearer:

number= 2345
numb_by_10= 2345/10= 234.5
int_numb_by_10= 234
diff= 234.5 - 234= .5
10xdiff= 10*.5= 5 -------------------------- first element of the
array variable.

And so forth.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default split a number into its digits

I know that there is the possibility of changing the data type of a
variable, but actually I'm not an expert in conversions...
I thought that declaring the variable digit() As Double could already
solve the problem of converting the string "5" in number 5...

Try with the other approach that I posted. It allows you to work with
numbers instead of strings, and maybe it's better. I don't have time
now to write it in proper code, because I'm just about to leave the
office. But I hope it can be a useful suggestion anyway :-)

See ya!

Antonio.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default split a number into its digits

Antonio! this is great! thank you so much. Have a great day!
Paco


"a.riva@UCL" wrote:

I know that there is the possibility of changing the data type of a
variable, but actually I'm not an expert in conversions...
I thought that declaring the variable digit() As Double could already
solve the problem of converting the string "5" in number 5...

Try with the other approach that I posted. It allows you to work with
numbers instead of strings, and maybe it's better. I don't have time
now to write it in proper code, because I'm just about to leave the
office. But I hope it can be a useful suggestion anyway :-)

See ya!

Antonio.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA write macro change column with 3 number digits to 4 digits the James C[_2_] Excel Discussion (Misc queries) 3 January 25th 10 03:12 PM
number 12 digits to 15 digits mehdy-e Excel Discussion (Misc queries) 5 November 13th 09 04:43 PM
How many number digits can I fit into one cell? HoppoM Excel Discussion (Misc queries) 3 August 13th 09 05:51 PM
Sum of digits in a number Bob Ptacek Excel Worksheet Functions 3 January 8th 08 12:49 AM
How to customize number to 10 digits including 2 digits after deci Carina Excel Worksheet Functions 3 September 20th 07 02:50 AM


All times are GMT +1. The time now is 08:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"