Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

VBA macro to average

Original Message
Name: randym44
Date: October 11, 2007 at 09:43:38 Pacific
Subject: VBA macro to average
OS: XP
CPU/Ram: Pent IV 1 GB
Model/Manufacturer: Dell
Comment:
My VBA knowledge is very basic so looking more for an answer than help with developing the code.

I typically have data files with 1 data point per week day that will sometimes be 15 years long. I would like to have a macro that will give me a monthly average of the daily data for each month. Obviously I can do it manually, but going thru 15 years of daily data, with different number of week days in each month, to calculate a monthly average for each is a pain. Any suggestions or help would be greatly appreciated.


Report Offensive Message For Removal


Response Number 1
Name: jhunt303
Date: October 14, 2007 at 07:43:58 Pacific
Subject: VBA macro to average
Reply: (edit)

'Hello
'Havent posted here for ages! So I'll do it properly

'Try something like this
'Assuming your data is in two columns
'A = Date, B = Day-Traffic
'And you have two sheets, sheet1 (s1) and sheet2 (s2)
'Where sheet1 is your RAW data and 2 is output
Dim i As Integer
Dim intOutRow As Integer


Sub PerMonth()
'Set you worksheets
Set s1 = Worksheets("sheet1")
Set s2 = Worksheets("sheet2")
'Initialise variables the first date in your set
intOutRow = 1 'Excel is not zero-based
intYear = Year(s1.Cells(1, 1).Value)
intMonth = Month(s1.Cells(1, 1).Value)
'Loop through your data
For i = 1 To 3000 'or length of data
'Check if date has changes during loop
If Year(s1.Cells(i, 1).Value) = intYear And _
Month(s1.Cells(i, 1).Value) = intMonth Then
'Month is same so add to values in sheet 2
s2.Cells(intOutRow, 1).Value = Str(intYear) + " - " + Str(intMonth)
s2.Cells(intOutRow, 2).Value = s2.Cells(intOutRow, 2).Value + s1.Cells(i, 2).Value
Else
'Increment the output row and reassign Year and Month
intOutRow = intOutRow + 1
intYear = Year(s1.Cells(i, 1).Value)
intMonth = Month(s1.Cells(i, 1).Value)
'Month is now the same so add to values in sheet 2
s2.Cells(intOutRow, 1).Value = Str(intYear) + " - " + Str(intMonth)
s2.Cells(intOutRow, 2).Value = s2.Cells(intOutRow, 2).Value + s1.Cells(i, 2).Value
End If
Next i
End Sub
'Ok, this code could be severly reduced in size, but then it might lose it's lesson!
'In addition you could clear the data from sheet2 before posting to it?
'Thanks


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: VBA macro to average

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12

help mandriva install last straw!

Icon Scaling in Explorer Bar


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC