SolvedPrivate

Arrays in UDFs for VBA Excel

Posted on 2015-02-16
8
19 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
8 Comments
 
LVL 12

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 12

Expert Comment

by:Jeff Darling
ID: 40613066
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

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 12

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 45

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now