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

I have a VBA function which returns a Variant(). This function is
called as an array formula within Excel.

Within the function VBA assigns an array to the function return value.
Somewhere in there I'd like to be able scalar multiply the array
without having to manually loop through each element.

Here's sample code illustrating what I'd *like* to be able do:

Public Function MyReturnArrayFunction() as Variant()
myRawProbs = Array(7, 9, 3)
' do more stuff here
MyReturnArrayFunction = Application.Transpose(myRawProbs)/19 '
This does not work!
End Function

I do realize I could simply loop through thr array and divide through
by the constant, but ostensibly when the VBA array is assigned to the
Excel range it's already looping through each element anyway -- so why
should I have to do that twice?

Thanks in advance,
Jacob
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Scalar Multiplying VBA Array

Jacob,

This works

Public Function MyReturnArrayFunction() As Variant()
Dim myRawProbs As String

myRawProbs = "{7,9,3}"
With Application
MyReturnArrayFunction = .Transpose(ActiveSheet.Evaluate(myRawProbs &
" / 19"))
End With
End Function

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Jacob JKW" wrote in message
...
I have a VBA function which returns a Variant(). This function is
called as an array formula within Excel.

Within the function VBA assigns an array to the function return value.
Somewhere in there I'd like to be able scalar multiply the array
without having to manually loop through each element.

Here's sample code illustrating what I'd *like* to be able do:

Public Function MyReturnArrayFunction() as Variant()
myRawProbs = Array(7, 9, 3)
' do more stuff here
MyReturnArrayFunction = Application.Transpose(myRawProbs)/19 '
This does not work!
End Function

I do realize I could simply loop through thr array and divide through
by the constant, but ostensibly when the VBA array is assigned to the
Excel range it's already looping through each element anyway -- so why
should I have to do that twice?

Thanks in advance,
Jacob



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Scalar Multiplying VBA Array

On Jan 3, 4:17 am, "Bob Phillips" wrote:
I have a VBA function which returns a Variant(). This function is
called as an array formula within Excel.
Within the function VBA assigns an array to the function return value.
Somewhere in there I'd like to be able scalar multiply the array
without having to manually loop through each element.


Here's sample code illustrating what I'd *like* to be able do:


Public Function MyReturnArrayFunction() as Variant()
myRawProbs = Array(7, 9, 3)
' do more stuff here
MyReturnArrayFunction = Application.Transpose(myRawProbs)/19 '
This does not work!
End Function


I do realize I could simply loop through thr array and divide through
by the constant, but ostensibly when the VBA array is assigned to the
Excel range it's already looping through each element anyway -- so why
should I have to do that twice?


Public Function MyReturnArrayFunction() As Variant()
Dim myRawProbs As String

myRawProbs = "{7,9,3}"
With Application
MyReturnArrayFunction = .Transpose(ActiveSheet.Evaluate(myRawProbs &
" / 19"))
End With
End Function

The problem here is that it requires expressing joining my array into
a string. That's not fast operation. The array itself is a calculated
variable -- I just expressed it as a constant to simplify the example
code.

Thanks,
Jacob.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Scalar Multiplying VBA Array

Public Function MyReturnArrayFunction() As Variant()
myRawProbs = Array(19, 38, 57)
ActiveSheet.Names.Add Name:="myRawProbs", RefersTo:=myRawProbs
MyReturnArrayFunction = _
Application.Transpose(ActiveSheet.Evaluate("myRawP robs /19"))
End Function

Alan Beban

Jacob JKW wrote:
On Jan 3, 4:17 am, "Bob Phillips" wrote:

I have a VBA function which returns a Variant(). This function is
called as an array formula within Excel.
Within the function VBA assigns an array to the function return value.
Somewhere in there I'd like to be able scalar multiply the array
without having to manually loop through each element.


Here's sample code illustrating what I'd *like* to be able do:


Public Function MyReturnArrayFunction() as Variant()
myRawProbs = Array(7, 9, 3)
' do more stuff here
MyReturnArrayFunction = Application.Transpose(myRawProbs)/19 '
This does not work!
End Function


I do realize I could simply loop through thr array and divide through
by the constant, but ostensibly when the VBA array is assigned to the
Excel range it's already looping through each element anyway -- so why
should I have to do that twice?


Public Function MyReturnArrayFunction() As Variant()
Dim myRawProbs As String

myRawProbs = "{7,9,3}"
With Application
MyReturnArrayFunction = .Transpose(ActiveSheet.Evaluate(myRawProbs &
" / 19"))
End With
End Function


The problem here is that it requires expressing joining my array into
a string. That's not fast operation. The array itself is a calculated
variable -- I just expressed it as a constant to simplify the example
code.

Thanks,
Jacob.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Scalar Multiplying VBA Array

On Jan 4, 12:31 am, Alan Beban wrote:
Public Function MyReturnArrayFunction() As Variant()
myRawProbs = Array(19, 38, 57)
ActiveSheet.Names.Add Name:="myRawProbs", RefersTo:=myRawProbs
MyReturnArrayFunction = _
Application.Transpose(ActiveSheet.Evaluate("myRawP robs /19"))
End Function

Alan Beban

Jacob JKW wrote:
On Jan 3, 4:17 am, "Bob Phillips" wrote:


I have a VBA function which returns a Variant(). This function is
called as an array formula within Excel.
Within the function VBA assigns an array to the function return value.
Somewhere in there I'd like to be able scalar multiply the array
without having to manually loop through each element.


Here's sample code illustrating what I'd *like* to be able do:


Public Function MyReturnArrayFunction() as Variant()
myRawProbs = Array(7, 9, 3)
' do more stuff here
MyReturnArrayFunction = Application.Transpose(myRawProbs)/19 '
This does not work!
End Function


I do realize I could simply loop through thr array and divide through
by the constant, but ostensibly when the VBA array is assigned to the
Excel range it's already looping through each element anyway -- so why
should I have to do that twice?


Public Function MyReturnArrayFunction() As Variant()
Dim myRawProbs As String


myRawProbs = "{7,9,3}"
With Application
MyReturnArrayFunction = .Transpose(ActiveSheet.Evaluate(myRawProbs &
" / 19"))
End With
End Function


The problem here is that it requires expressing joining my array into
a string. That's not fast operation. The array itself is a calculated
variable -- I just expressed it as a constant to simplify the example
code.


Thanks,
Jacob.


Thanks for the reply, Alan. Calling this function from Excel:
Public Function MyReturnArrayFunction() As Variant()
On Local Error GoTo ErrHandler
Dim myRawProbs() As Variant
myRawProbs = Array(19, 38, 57)
ActiveSheet.Names.Add Name:="myRawProbs", RefersTo:=myRawProbs
MyReturnArrayFunction = _
Application.Transpose(ActiveSheet.Evaluate("myRawP robs /19"))
Exit Function
ErrHandler:
Debug.Print Err.Description & " (" & Err.Number & ")"
End Function

I get "Application-defined or object-defined error (1004)" as a debug
message. The error is caused by the statement:
ActiveSheet.Names.Add Name:="myRawProbs", RefersTo:=myRawProbs

Thanks,
Jacob


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Scalar Multiplying VBA Array

Jacob JKW wrote:
Thanks for the reply, Alan. Calling this function from Excel:
Public Function MyReturnArrayFunction() As Variant()
On Local Error GoTo ErrHandler
Dim myRawProbs() As Variant
myRawProbs = Array(19, 38, 57)
ActiveSheet.Names.Add Name:="myRawProbs", RefersTo:=myRawProbs
MyReturnArrayFunction = _
Application.Transpose(ActiveSheet.Evaluate("myRawP robs /19"))
Exit Function
ErrHandler:
Debug.Print Err.Description & " (" & Err.Number & ")"
End Function

I get "Application-defined or object-defined error (1004)" as a debug
message. The error is caused by the statement:
ActiveSheet.Names.Add Name:="myRawProbs", RefersTo:=myRawProbs

Thanks,
Jacob


Yes; called from a worksheet, a Function can't change the Excel
environment, i.e., can't add a worksheet name. I didn't appreciate that
that's what was happening.

I'm not sure I understand the flow, but

Public Function MyReturnArrayFunction() As Variant()
On Local Error GoTo ErrHandler
Dim myRawProbs() As Variant
myRawProbs = Array(19, 38, 57)
MyReturnArrayFunction = _
Application.Transpose(myRawProbs)
Exit Function
ErrHandler:
Debug.Print Err.Description & " (" & Err.Number & ")"
End Function

Can be called from the worksheet with

=MyReturnArrayFunction()/19, array entered, to return the desired result.

Alan Beban
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Scalar Multiplying VBA Array

Or, if the functions in the freely downloadable file at
http://home.pacbell.net/beban are available to your workbook,

Public Function MyReturnArrayFunction(iScalar As Single) As Variant()
On Local Error GoTo ErrHandler
Dim myRawProbs() As Variant
myRawProbs = Array(19, 38, 57)
MyReturnArrayFunction = _
ScalarMult(Application.Transpose(myRawProbs), iScalar, False)
Exit Function
ErrHandler:
Debug.Print Err.Description & " (" & Err.Number & ")"
End Function

called as

=MyReturnArrayFunction(19)

will do so.

Alan Beban
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Scalar Multiplying VBA Array

On Jan 4, 12:44 pm, Alan Beban wrote:
Or, if the functions in the freely downloadable file at http://home.pacbell.net/bebanare available to your workbook,

Public Function MyReturnArrayFunction(iScalar As Single) As Variant()
On Local Error GoTo ErrHandler
Dim myRawProbs() As Variant
myRawProbs = Array(19, 38, 57)
MyReturnArrayFunction = _
ScalarMult(Application.Transpose(myRawProbs), iScalar, False)
Exit Function
ErrHandler:
Debug.Print Err.Description & " (" & Err.Number & ")"
End Function

called as

=MyReturnArrayFunction(19)

will do so.

Alan Beban

I haven't tried your suggestion yet, but there does look to be some
pretty cool functions in your spreadsheet Thanks much for the link,
Alan.
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Scalar Multiplying VBA Array

You're welcome; thanks for the feedback.

Alan

Jacob JKW wrote:
On Jan 4, 12:44 pm, Alan Beban wrote:

Or, if the functions in the freely downloadable file at http://home.pacbell.net/bebanare available to your workbook,

Public Function MyReturnArrayFunction(iScalar As Single) As Variant()
On Local Error GoTo ErrHandler
Dim myRawProbs() As Variant
myRawProbs = Array(19, 38, 57)
MyReturnArrayFunction = _
ScalarMult(Application.Transpose(myRawProbs), iScalar, False)
Exit Function
ErrHandler:
Debug.Print Err.Description & " (" & Err.Number & ")"
End Function

called as

=MyReturnArrayFunction(19)

will do so.

Alan Beban


I haven't tried your suggestion yet, but there does look to be some
pretty cool functions in your spreadsheet Thanks much for the link,
Alan.

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
multiplying wineforyou Excel Worksheet Functions 5 November 15th 09 05:35 PM
Multiplying Dave Excel Worksheet Functions 1 November 28th 07 09:27 AM
Multiplying Contiguous Values in an Array [email protected] Excel Worksheet Functions 1 May 26th 07 06:46 AM
Array formula and multiplying conditions KR Excel Worksheet Functions 6 September 22nd 06 12:33 AM
Multiplying in a row Daniel - Sydney Excel Discussion (Misc queries) 3 September 27th 05 12:24 AM


All times are GMT +1. The time now is 10:11 PM.

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"