Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default simple function question

OK, I can't get this function to return anything other than #VALUE! What am
I doing wrong? The function is in a module, and the input parameter is a
reference to a cell containing an integer.

Function my_digit_patt(a) As Integer
Dim ct(9) As Integer
For i = 1 To Len(a)
ct(Val(Mid(a, i, 1))) = ct(Val(Mid(a, i, 1))) + 1
Next
b = 0
For i = 0 To 10
b = b + ct(i) * 10 ^ (10 - i)
Next
my_digit_patt = b
End Function


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default simple function question

Don,

This seem to work...
'---------------------------
Function my_digit_patt(ByRef a As Variant) As Double
Dim i As Long
Dim b As Double
Dim ct(0 To 9) As Long

For i = 1 To Len(a)
ct(Val(Mid(a, i, 1))) = ct(Val(Mid(a, i, 1))) + 1
Next 'i

b = 0
For i = 0 To Len(a)
b = b + ct(i) * 10 ^ (10 - i)
Next 'i

my_digit_patt = b
End Function
'---'

Sub GetIt()
MsgBox my_digit_patt(110)
End Sub

'-------------------------------------
Jim Cone
San Francisco, USA


"Don Shepherd" wrote in
message ...
OK, I can't get this function to return anything other than #VALUE! What am
I doing wrong? The function is in a module, and the input parameter is a
reference to a cell containing an integer.
Function my_digit_patt(a) As Integer
Dim ct(9) As Integer
For i = 1 To Len(a)
ct(Val(Mid(a, i, 1))) = ct(Val(Mid(a, i, 1))) + 1
Next
b = 0
For i = 0 To 10
b = b + ct(i) * 10 ^ (10 - i)
Next
my_digit_patt = b
End Function
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default simple function question

On Fri, 10 Jun 2005 21:55:17 -0400, "Don Shepherd" wrote:

OK, I can't get this function to return anything other than #VALUE! What am
I doing wrong? The function is in a module, and the input parameter is a
reference to a cell containing an integer.

Function my_digit_patt(a) As Integer
Dim ct(9) As Integer
For i = 1 To Len(a)
ct(Val(Mid(a, i, 1))) = ct(Val(Mid(a, i, 1))) + 1
Next
b = 0
For i = 0 To 10
b = b + ct(i) * 10 ^ (10 - i)
Next
my_digit_patt = b
End Function


Well, I see a few problems off the top of my head.

1. On line 7: For i = 0 To 10
You are using i as the subscript for ct. But the upper bound of ct is
only 9. So when i=10, the subscript will be out of range.

2. You have specified that your function should return an integer. But the
allowable range for integers in VBA is -32,768 to 32,767. Depending on the
range of values being passed to the function, the result may be out of range of
your data type.


--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default simple function question

Hey, thanks Jim and Ron, your help is much appreciated. Yes, I noticed the
out of range subscript after I posted and fixed it, but the problem
persisted. The real culprit, as you suggested, was using integer data type
when I wanted a 10 digit integer result. I switched to double and it worked
like a charm.

Thanks again guys, you are a lifesaver!

"Don Shepherd" wrote in message
...
OK, I can't get this function to return anything other than #VALUE! What

am
I doing wrong? The function is in a module, and the input parameter is a
reference to a cell containing an integer.

Function my_digit_patt(a) As Integer
Dim ct(9) As Integer
For i = 1 To Len(a)
ct(Val(Mid(a, i, 1))) = ct(Val(Mid(a, i, 1))) + 1
Next
b = 0
For i = 0 To 10
b = b + ct(i) * 10 ^ (10 - i)
Next
my_digit_patt = b
End Function




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
Simple Sum function question Brwnize1 Excel Worksheet Functions 3 October 24th 07 04:50 PM
Probably simple question on AVG function Mike612 Excel Discussion (Misc queries) 1 July 26th 07 03:14 AM
Simple Function question? jboss73 Excel Worksheet Functions 3 January 6th 06 02:22 PM
Simple Simple Excel usage question BookerW Excel Discussion (Misc queries) 1 June 23rd 05 10:06 PM
Simple Function question Jeff Wright[_2_] Excel Programming 6 May 1st 05 12:44 AM


All times are GMT +1. The time now is 07:17 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"