SolvedPrivate

Arrays in UDFs for VBA Excel

Posted on 2015-02-16
8
23 Views
Last Modified: 2016-02-10
Hello Experts-

I have a UDF that pulls variables to calculate a customer's annual utility bill. The UDF references 12 months of usage data, each month is an individual cell reference which means 12 different variables in the UDF (plus the other ones needed). I've tried to name them as a range and simplify the UDF but have had no success. So, the question is, how can I pull in the 12 months of data as an array, have it cycle through the calculations and then spit out one annual number? Do I have name the range in Excel and reference that in the UDF cell equation?I have a copy of the spreadsheet attached.

Also, for some reason, I am unable to show more than one subroutine at a time in each module (I have to use the drop down box where is says declarations.) Is there a way to change this back?

Regards,
Todd
0
Comment
Question by:TCristiano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Accepted Solution

by:
Jeff Darling earned 250 total points
ID: 40613064
One way to do that would be to build a delimited string with your parameters and then use the split function to create an array.

Here is a test function that passes the delimited string.

Public Function DoMyCalc(myData As String, strName)

Dim myRetVal
Dim ArrMyData

ArrMyData = Split(myData, "|")

myRetVal = 0

For i = 0 To UBound(ArrMyData)
  myRetVal = myRetVal + ArrMyData(i)
Next

DoMyCalc = strName & " " & myRetVal

End Function

Open in new window


Sample excel file attached
0
 
LVL 40
ID: 40613065
I can't help you with UDF, I have never used them, doing everything in VBA.

But for the modules, there are 2 toggle buttons in the lower left corner of the editor that lets you switch between the 2 different views.
0
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 40613066
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Jeff Darling
ID: 40613077
buttons that Jacques referred to.
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 250 total points
ID: 40613089
If you reference a range object, you can assign a Variant data type variable = rangeobject.value

This will populate the Variant variable with a 2D array.
0
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 40613119
Note that the 2D Array isn't zero based, it starts with 1.

Here is a sample based on aikimark's suggestion

Public Function foo(MyRange As Range, strName)

Dim myRetVal
Dim ArrMyRange

ArrMyRange = MyRange.Value
myRetVal = 0

For i = 1 To UBound(ArrMyRange,2) 
  myRetVal = myRetVal + ArrMyRange(1, i)
Next

foo = strName & " " & myRetVal

End Function

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40667527
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel 2007 - Automate data validation and send via email. 3 31
Embed Excel WS into PPTX 4 26
Batch convert csv to xlsx 10 62
MS Access 2016 resize forms 3 12
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question