Averaging unique ID values in VBA

MurdochQuill

Board Regular
Joined
Nov 21, 2020
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Trying to average some unique ID's in VBA.

I use an in-cell formula but I would like to make it part of a macro, I have
Here is formula.
=IF(A2="","",AVERAGEIF($A$2:$A$200,A2,$B$2:$B$200))

Column A is the ID, column B is the value. Formula is on row C. EG:

IDValueAverage of Unique ID (Above formula)
11013
11613
222
3511.66
31011.66
32011.66
41010

Here is a snippet that can sort the unique ID's, but I'm not sure how to rejig it to give average instead of counting the amount of ID's.
VBA Code:
Sub CountUniqueValues()
Dim LstRw As Long, Rng As Range, List As Object, ListCount As Long
LstRw = Cells(Rows.Count, "B").End(xlUp).Row
Set List = CreateObject("Scripting.Dictionary")

For Each Rng In Range("B6:B" & LstRw)
  If Not List.Exists(Rng.Value) Then List.Add Rng.Value, Nothing
Next

ListCount = List.Count

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Why do you want that in a macro? The formula will be quicker.
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,342
Members
448,570
Latest member
rik81h

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