[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Monthly average - VBA

Posted on 2011-10-25
Medium Priority
Last Modified: 2012-05-12
I have came up with my own solution for this but I am not if its the neatest approach to do it.

all I am trying to do is to get the monthly average of the price for the dates and populate against the cells. (in VBA)
Please see the attahed file.
It would be great if some one provide me with code that would populate the 'Monthly Avg' - Column C with the price average for that month.

Many thanks
Question by:SameerMirza
  • 3
  • 3
LVL 50
ID: 37023410

any particular reason for VBA? A formula would be much faster. Start in C2


Confirm with Ctrl-Shift-Enter and copy down.

cheers, teylyn
LVL 50

Accepted Solution

Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 2000 total points
ID: 37023431
Here's one possibility to do it in code

Sub Macro2()
Dim lRow As Long
lRow = Cells(Rows.Count, "A").End(xlUp).Row
    Range("C2").FormulaArray = _
        "=AVERAGE(IF(MONTH($A$2:$A$" & lRow & ")=MONTH(A2),$B$2:$B$" & lRow & ",""""))"
    Range("C2").AutoFill Destination:=Range("C2:C" & lRow)
    With Range("C2:C" & lRow)
        .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    End With
    Application.CutCopyMode = False
End Sub

Open in new window


Author Comment

ID: 37023625
Thanks but in this case the records would go beyond one year
That is multiple years.
so we would have to add check for multiple years too.
I tried,

=AVERAGE(IF(AND(YEAR($A$2:$A$24)=YEAR(A2), MONTH($A$2:$A$24)=MONTH(A2)),$B$2:$B$24,""))

But it fails. Because its trying to locate the same value in the whole range.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 50
ID: 37023664
AND() does not work in this context. You can use this instead:


Remember to confirm with Ctrl-Shift-Enter

cheers, teylyn

Author Comment

ID: 37024013
Thanks. That seems to work fine
Although I have written a module too (which works :)) using loops but I think your would be much quicker.

I have another very quick one.
I am trying to freeze pans.
Go select row -> window -> freeze pane -
for some reason it is freezing on 16 rows

tried doing it from VBA too. Get the same result
'ActiveWindow.FreezePanes = True'

How can I do it from VBA such that it would freeze on header/first row?


Author Closing Comment

ID: 37024043
Please dont bother reading what i just asked. :)
you are suppose to select the data and I was selecting the first row :/

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

872 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