ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA Functions (https://www.excelbanter.com/excel-programming/395754-vba-functions.html)

LesHurley

VBA Functions
 
Whats wrong with this? I get a #VALUE error:
Function DotProd(Vector1)
DotProd = Rows(Vector1)
End Function
--
Thanks for your help

Bill Renaud

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



Ron Rosenfeld

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

Ron Rosenfeld

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

LesHurley

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


Dana DeLouis

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





All times are GMT +1. The time now is 07:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com