473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing Discontinuous Range (Array) of Values to User-Defined Function

What I am trying to do seems simple, but I haven't figured out how to
do it. I need to add the contents of the cells in one array, subject
to the values in another array. Normally I would utilize one of the
SUM functions, such as SUM, SUMIF, or SUMPRODUCT. However, they do
not work when the elements of the arrays are on multiple sheets in the
Excel workbook. For example:

Array 1: Sheet1:Sheet5!A4 (which contains the values: 1, 0, 0, 1,
0)
Array 2: Sheet1:Sheet5!B4 (which contains the values: 150, 324,
846, 254, 98)

I need to compute the sum of the items in Array 2 whose
corresponding items in Array 1 are equal to 1. In the example above,
the value I would expect to obtain is 404, which is equal to 150 + 254
(the only items in Array 2 whose corresponding items in Array 1 are
equal to 1).

I have tried the following VBA code (and several other
permutations) to no avail. And I don't understand why the result of
the UBound function is zero. Can anyone help me with this?

Function ConditionalSum(testArray, ParamArray argumentArray() As
Variant) As Double
Dim currentRange As Range
Dim currentArea As Range
Dim i As Integer
Dim col As Integer
Dim row As Integer
Dim rowCount As Integer
Dim colCount As Integer
Dim areaCount As Integer
Dim sum As Double

sum = 0#
MsgBox ("UBound = " & UBound(argumentArray))
For i = 1 To UBound(argumentArray)
Set currentRange = argumentArray(i)
Set testRange = testArray(i)

rowCount = currentRange.Rows.Count
colCount = currentRange.Columns.Count

For col = 1 To colCount
For row = 1 To rowCount
If testRange(row, col) = 1 Then
sum = sum + currentRange(row, col)
End If
Next row
Next col
Next i

ConditionalSum = sum

End Function
Nov 12 '05 #1
2 6510
Hi Louis,

VBA does not understand multi-sheet ranges: your UDF is not going to work.

You would have have to use character arguments and decode them.

Charles
______________________
Decision Models
FastExcel Version 2 now available.
www.DecisionModels.com/FxlV2WhatsNew.htm
Nov 12 '05 #2
"Charles Williams" <Ch*****@DecisionModels.com> wrote...
VBA does not understand multi-sheet ranges: your UDF is not going to work.

You would have have to use character arguments and decode them.


Impractical. OP would need to write a lightweight range reference parser
since there's nothing in VBA that can accomodate converting the text

"'foo:bar'!X99"

into anything useful other than 'foo'!X99 and 'bar'!X99 separately. But that
gives an alternative approach: pass 3D references to UDFs using two
arguments rather than one: top-upper-left and bottom-lower-right cells. So
to replace the built-in function (the following call doesn't work)

=SUMIF(foo:bar!X99:Y200,crit)

with a UDF call, consider calling syntax like

=SUMIF3D(foo!X99,bar!Y200,crit)

The advantage of this is that you can use (2D) range references as arguments
rather than having to construct 3D references as strings in the UDF call
argument then immediately deconstruct them in the UDF.
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
5
by: Raju V.K | last post by:
I am developing a web site which requires a set og unique values be transferred between pages and server many times. There fore i decided to put the values in an array so that only one reference to...
0
by: Daniel Chartier | last post by:
Hello, all. I found a bit of code that lets me read an Excel file from and to a specifi range. See the code below. But what if I only want to specify the STARTING position and simply want it...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
2
by: craigkenisston | last post by:
Hi, I created an array of objects like this : object Values = {myObject.myprop, otherobject.otherprop, thirdobject.xprop}; Then I pass it to a method. and I get the values filled in that...
2
by: PK | last post by:
Hi, I have an application that opens a Crystal report document and passes in a value to a parameter in the report (pointing to an Oracle DB). However, if I want to pass a "null" value to retrieve...
2
by: George | last post by:
Is there a fast way to transfer an Excel range to an array? Example: Excel range is E2:E300 Dim person() as string Thanks, George
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
6
by: JackM | last post by:
I have a multiple select input in a form that's being populated by a row from my database as such: <input type=\"checkbox\" name=\"subm\" value=\"$row\"> That part is working fine as I can check...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.