vba arithmetic between ranges without looping thru each item

jalea148

Board Regular
Joined
Mar 23, 2012
Messages
58
Consider the function xyz
Code:
Function xyz(Opn As Range, Clo As Range, Wts As Range)
Dim MaxOpnClo As Range
    MaxOpnClo = Application.WorksheetFunction.Max(Opn, Clo)
    xyz= Application.WorksheetFunction.SumProduct(MaxOpnClo, Wts) / Application.WorksheetFunction.Sum(Wts)
    
End Function

As written, this code does not work.

Presumably MaxOpnClo = max(Opn,Clo), ...
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi,

A few questions, if I may, please:

Why don't you want to use a loop?
Will this be a User Defined Function that you will use on a Worksheet?
Isn't Application.WorksheetFunction.SumProduct(MaxOpnClo, Wts) / Application.WorksheetFunction.Sum(Wts) just MaxOpnClo?

Thanks,
 
Upvote 0
Also, what are you trying to do with those 2 blue lines?
Max will return a numerical value. You can't set that to be a Range object.

Code:
Function xyz(Opn As Range, Clo As Range, Wts As Range)
[COLOR="#0000CD"]Dim MaxOpnClo As Range
    MaxOpnClo = Application.WorksheetFunction.Max(Opn, Clo)[/COLOR]
    xyz= Application.WorksheetFunction.SumProduct(MaxOpnClo, Wts) / Application.WorksheetFunction.Sum(Wts)
    
End Function
Tell us more about what you are actually trying to achieve with this function and what your data is like that you want to use it on.
 
Upvote 0
I finally resolved the problem by going to loops. Max between 2 identically dimensioned ranges is the essence of my question. Think how much cleaner and less prone to error it would be if the operation on the range object could be treated aS in linear algebra.
Thanx for responding.
 
Upvote 0
But this does work?
Code:
Sub Test()
    Dim MaxOpnClo As Double
    Dim Opn As Range
    Dim Clo As Range
    
    Set Opn = Range("A1:C1")
    Set Clo = Range("A2:C2")
    MaxOpnClo = Application.WorksheetFunction.Max(Opn, Clo)
End Sub
 
Upvote 0
But this does work?
Code:
Sub Test()
    Dim MaxOpnClo As Double
    Dim Opn As Range
    Dim Clo As Range
    
    Set Opn = Range("A1:C1")
    Set Clo = Range("A2:C2")
    MaxOpnClo = Application.WorksheetFunction.Max(Opn, Clo)
End Sub

Your example finds the max of the 6 cells. My example, below, fails. I want a new range with its 1st cell the max of the 1st pair of cells from Opn, Clo; 2nd cell the max of the 2nd pair of cells from Opn, Clo; ...
Function gmax(Opn As Range, Clo As Range, Wts As Range)
Dim MaxOpnClo As Range, WtsA() As Variant
Dim I As Integer, WtsSize As Long, SumWta As Long

WtsA = Wts
WtsSize = UBound(WtsA, 1)

SumWta = WtsSize * (WtsSize + 1) / 2
Debug.Print WtsSize, SumWta

MaxOpnClo = Application.WorksheetFunction.Max(Opn, Clo)
Debug.Print "hi"
.................
End Function
Debug.Print WtsSize, SumWta is correct.
Debug.Print "hi" does not print
It's necessary to loop through the individual pairs.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top