VBA user defined function result to spill in two columns

dipkchopra

New Member
Joined
Nov 19, 2013
Messages
13
Hi all,
I am trying to create a user defined function using VBA.
The goal is to create a formula based pivot out of two columns - one containing names and the other containing amounts.
The formula should throw out unique list of names in first column and sum of those names in second column


I tried creating Function Sumpivot(getnames As Range, getamounts As Range)

Code to get first column of my answer = Application.Unique(getnames)
and code for second column = Application.SumIfs(getamounts, getnames, Application.Unique(getnames))
I tried - sumpivot = Application.Unique(getnames) & Application.SumIfs(getamounts, getnames, Application.Unique(getnames)) - this does not work
My problem is how do I load both of these in one output?


This is what I am trying to achieve:
DATA:
C
1​
B
2​
B
3​
A
4​
A
1​
A
2​
Output
C
1​
B
5​
A
7​

Please guide
and thanks in advance.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You can do it like
VBA Code:
Function Sumpivot(getnames As Range, getamounts As Range) As Variant
   Dim a, b
   a = Application.Unique(getnames)
   b = Application.SumIfs(getamounts, getnames, a)
   Sumpivot = Application.Choose(Array(1, 2), a, b)
End Function
but why not just use a formula, it will be quicker.
 
Upvote 0
Solution
You can do it like
VBA Code:
Function Sumpivot(getnames As Range, getamounts As Range) As Variant
   Dim a, b
   a = Application.Unique(getnames)
   b = Application.SumIfs(getamounts, getnames, a)
   Sumpivot = Application.Choose(Array(1, 2), a, b)
End Function
but why not just use a formula, it will be quicker.
yes but if I have to do this over and over again, UDF will be quicker and easier
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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