Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default VBA Functions

Whats wrong with this? I get a #VALUE error:
Function DotProd(Vector1)
DotProd = Rows(Vector1)
End Function
--
Thanks for your help
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default VBA Functions

A function has to have a return type (long, integer, double, string,
etc.) depending on the value of what you are returning.
Also, exactly what is Vector1? Is it a range of cells or an array of
values? What about DotProd? Is it the product of all of the values of
the cells in Vector1? Show us the math that would normally be done.

One possible interpretation (assuming Vector1 is a Range) (not tested):

Public Function DotProd(Vector1 as Range) as Double
Dim rngCell as Range
Dim dblDotProd as Double

dblDotProd = 1

For each rngCell in Vector1
dblDotProd = dblDotProd*rngCell.Value
next rngCell

DotProd = dblDotProd
End Function
--
Regards,
Bill Renaud


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default VBA Functions

On Thu, 16 Aug 2007 18:19:46 -0700, LesHurley
wrote:

Whats wrong with this? I get a #VALUE error:
Function DotProd(Vector1)
DotProd = Rows(Vector1)
End Function


What's "wrong" is that you are trying to return an object.

Without knowing what you are trying to do, it's hard to know how to advise you.

If you want a count of the rows in Vector 1, then try:

DotProd = Vector1.Rows.Count

If you want something else, you'll have to let us know.
--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default VBA Functions

On Thu, 16 Aug 2007 21:20:17 -0500, "Bill Renaud"
wrote:

A function has to have a return type (long, integer, double, string,
etc.)


A function will have a return type, but if it is not specified, as in the OP's
post, the return type will be a Variant.
--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default VBA Functions

Thanks everyone for your suggestions. Actually, the code I posted is a very
simple and complete version of of what I'm trying to do. You no doubt have
guessed that I am a complete new-comer to VBA programming. After a lot of
head scratching I came up with the code that Ron suggested and it works fine.
The function name is unimportant but it will eventually be a dotproduct
routine that doesn't care about the orientation of the two vectors. I'm
nearly finished with that.

My next project will be to row-reduce a matrix to canonical and echelon
form. In that case I will have to return another matrix, and I havent
figured out how to do that. Any suggestions would be welcome.


--
Thanks for your help


"Ron Rosenfeld" wrote:

On Thu, 16 Aug 2007 18:19:46 -0700, LesHurley
wrote:

Whats wrong with this? I get a #VALUE error:
Function DotProd(Vector1)
DotProd = Rows(Vector1)
End Function


What's "wrong" is that you are trying to return an object.

Without knowing what you are trying to do, it's hard to know how to advise you.

If you want a count of the rows in Vector 1, then try:

DotProd = Vector1.Rows.Count

If you want something else, you'll have to let us know.
--ron



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default VBA Functions

DotProd = Rows(Vector1)

Just throwing out two ideas.

Sub Demo()
Dim v, d
v = [A1:A3].Value
d = WorksheetFunction.SumSq(v)
'or
d = DotProduct(v, v)
End Sub

Function DotProduct(x, y)
DotProduct = WorksheetFunction.SumProduct(x, y)
End Function

--
Dana DeLouis


"LesHurley" wrote in message
...
Thanks everyone for your suggestions. Actually, the code I posted is a
very
simple and complete version of of what I'm trying to do. You no doubt
have
guessed that I am a complete new-comer to VBA programming. After a lot of
head scratching I came up with the code that Ron suggested and it works
fine.
The function name is unimportant but it will eventually be a dotproduct
routine that doesn't care about the orientation of the two vectors. I'm
nearly finished with that.

My next project will be to row-reduce a matrix to canonical and echelon
form. In that case I will have to return another matrix, and I havent
figured out how to do that. Any suggestions would be welcome.


--
Thanks for your help


"Ron Rosenfeld" wrote:

On Thu, 16 Aug 2007 18:19:46 -0700, LesHurley
wrote:

Whats wrong with this? I get a #VALUE error:
Function DotProd(Vector1)
DotProd = Rows(Vector1)
End Function


What's "wrong" is that you are trying to return an object.

Without knowing what you are trying to do, it's hard to know how to
advise you.

If you want a count of the rows in Vector 1, then try:

DotProd = Vector1.Rows.Count

If you want something else, you'll have to let us know.
--ron



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
How to convert cell formula functions to code functions Adnan Excel Discussion (Misc queries) 1 October 1st 08 08:30 PM
efficiency: database functions vs. math functions vs. array formula nickname Excel Discussion (Misc queries) 2 July 14th 06 04:26 AM
Looking for a site with functions that substitute the ATP functions Franz Verga Excel Worksheet Functions 3 June 24th 06 04:30 AM
Conversion from Spreadsheet Toolkit functions (ESSV....) to EssBase API functions sujay Excel Programming 0 June 5th 06 10:13 AM
excel functions and User defined functions Kanan Excel Programming 4 May 20th 04 11:21 PM


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