SolvedPrivate

Arrays in UDFs for VBA Excel

Posted on 2015-02-16
8
21 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 46

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

773 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