Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Strange error on function

Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I get
the #value error. I'm not sure why because I set width as byte. Is there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly appreciated.

Thanks in advance.

Herman
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Strange error on function

a byte is either 0 or 1. So if you enter anything else, you would have an
invalid argument. Unless a 0 or 1 is all you want, then you should use
something else (integer, long, single, double).

--
Regards,
Tom Ogilvy

"Herman" wrote in message
...
Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I get
the #value error. I'm not sure why because I set width as byte. Is there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly

appreciated.

Thanks in advance.

Herman



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default Strange error on function

1 or 0 seems to me like a bit. I think any combination which would result in
something 255 will give #VALUE

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel

"Tom Ogilvy" wrote in message
...
a byte is either 0 or 1. So if you enter anything else, you would have an
invalid argument. Unless a 0 or 1 is all you want, then you should use
something else (integer, long, single, double).

--
Regards,
Tom Ogilvy

"Herman" wrote in message
...
Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I
get
the #value error. I'm not sure why because I set width as byte. Is
there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly

appreciated.

Thanks in advance.

Herman





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Strange error on function

Oops,
Ignore that, I was thinking of Bit.

anyway, I think you are overflowing your variables, so this worked for me
(up to arguments of magnitude 255)

Function volume(height As Byte, width As Byte, Optional length As Variant)
If IsMissing(length) Then
volume = CLng(height) * CLng(width)
Else
volume = CLng(height) * CLng(width) * CLng(length)
End If
End Function


--
Regards,
Tom Ogilvy



"Tom Ogilvy" wrote in message
...
a byte is either 0 or 1. So if you enter anything else, you would have

an
invalid argument. Unless a 0 or 1 is all you want, then you should use
something else (integer, long, single, double).

--
Regards,
Tom Ogilvy

"Herman" wrote in message
...
Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As

Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I

get
the #value error. I'm not sure why because I set width as byte. Is

there
something wrong with my code that I can't see? This is a sample that

I'm
playing with but it's driving me crazy. Any help will be greatly

appreciated.

Thanks in advance.

Herman





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default Strange error on function

Hi Herman,

Probably one of your arguments is text or another invalid data type.
BTW, Optional arguments should not have a type declaration; if you omit the
argument, it will be treated as zero, not as Missing.

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel

"Herman" wrote in message
...
Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I get
the #value error. I'm not sure why because I set width as byte. Is there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly
appreciated.

Thanks in advance.

Herman






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default Strange error on function

Hi Herman,

Probably one of your arguments is text or another invalid data type.
BTW, Optional arguments should not have a type declaration; if you omit the
argument, it will be treated as zero, not as Missing.

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel

"Herman" wrote in message
...
Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I get
the #value error. I'm not sure why because I set width as byte. Is there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly
appreciated.

Thanks in advance.

Herman



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Strange error on function

Couple of things...

First, your IsMissing() will never be true, since IsMissing only
operates on Variants, and length is defined as Single. Therefore any
call without length specified will return zero.

you don't really save anything by using Bytes rather than Longs or
Doubles. XL's Math operations are done using Longs or Doubles, so the
function is less efficient due to implicit coercion. True it will take a
few more bytes, but only a few. And the value returned to XL will be a
double, since that's how XL stores numbers.

As far as your error goes, using Bytes casts the result of your
calculation as a byte, so you'll get an overflow error if your height
times width exceed 256. This is indicated in the UDF return as #VALUE!


I can't reproduce your error, but I'd do it this way:

Public Function Volume2(height As Double, _
width As Double, Optional length As Double = -1) As Double
If length < 0 Then length = width
Volume2 = length * width * height
End Function




In article ,
Herman wrote:

Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I get
the #value error. I'm not sure why because I set width as byte. Is there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly appreciated.

Thanks in advance.

Herman

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Strange error on function

Function volume(height As Integer, width As Integer, Optional Length As
Variant)

Optional argument is declared as variant.

"Herman" wrote:

Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I get
the #value error. I'm not sure why because I set width as byte. Is there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly appreciated.

Thanks in advance.

Herman

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 318
Default Strange error on function

Just a technicality, Volume has to have all three - Height,Width and Length.
There is no option whatsoever!!
Alok Joshi

"Herman" wrote:

Hi,

I have the following function:

Function volume(height As Byte, width As Byte, Optional length As Single)
If IsMissing(length) Then
volume = height * width
Else
volume = height * width * length
End If
End Function

If I type in a number which is greater than 1 in the width argument, I get
the #value error. I'm not sure why because I set width as byte. Is there
something wrong with my code that I can't see? This is a sample that I'm
playing with but it's driving me crazy. Any help will be greatly appreciated.

Thanks in advance.

Herman

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
strange error martinbarnes Excel Discussion (Misc queries) 3 April 1st 08 09:02 PM
Strange error with basic divide function Brent Excel Worksheet Functions 1 June 28th 06 09:20 PM
Strange error Andy Setting up and Configuration of Excel 1 April 18th 06 08:11 PM
Very strange error superkopite Excel Discussion (Misc queries) 3 February 19th 06 11:21 AM
Strange error Filips Benoit Excel Programming 7 October 20th 04 08:02 PM


All times are GMT +1. The time now is 08:00 PM.

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

About Us

"It's about Microsoft Excel"