Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I added a name to your Data sheet ("Data") and a Summary Sheet ("Summary").
then I copies row 10 on the data sheet to the Lastrow +1 of the Summary sheet. Sub CalcAverage() StartRow = 12 'loop forever With Sheets("Data") Set CopyRange = .Range("A10:AZ10") 'Get Last row LastRow = .Range("A" & Rows.Count).End(xlUp).Row 'only collect data when 30 rows are reached If (LastRow - StartRow) + 1 = 30 Then 'perform averages on last 30 rows FirstRow = LastRow - 30 + 1 For ColCount = 1 To Range("AZ10").Column Set AverageRange = _ .Range(.Cells(FirstRow, ColCount), _ .Cells(LastRow, ColCount)) ColAverage = WorksheetFunction.Average(AverageRange) .Cells(10, ColCount) = ColAverage Next ColCount End If End With With Sheets("Summary") 'copy averages to new worksheet LastRow = .Range("A" & Rows.Count).End(xlUp).Row CopyRange.Copy Destination:=.Range("A" & (LastRow + 1)) End With Application.OnTime Now + _ TimeValue("00:00:03"), "CalcAverage" End Sub "soccerdav2003" wrote: Hi Joel, I got everything to work...pretty much. There's just one more thing I'm trying to do now and I'm having trouble getting it to work. I want to export average to another spreadsheet and have it index on the spreadsheet so whenever I run the macro it will record the average of all the values in the row and write to a another spreadsheet on a specific sheet and index to the next row on that sheet so I'm appending the exported data, not overwriting it. Here's how I want the data to look: Average Values for Increasing Flow: Volume Power q" Pressure Flow Psaturation 170 200 50 0.090 150 3.03 170 200 50 0.138 200 3.26 170 200 50 0.100 250 3.80 170 200 50 0.153 300 3.64 "Joel" wrote: Here is a better way of doing it. Call the function once. It will call itself every 3 seconds Sub CalcAverage() StartRow = 12 'loop forever 'Get Last row LastRow = Range("A" & Rows.Count).End(xlUp).Row 'only collect data when 30 rows are reached If (LastRow - StartRow) + 1 = 30 Then 'perform averages on last 30 rows FirstRow = LastRow - 30 + 1 For ColCount = 1 To Range("AZ10").Column Set AverageRange = _ Range(Cells(FirstRow, ColCount), _ Cells(LastRow, ColCount)) ColAverage = WorksheetFunction.Average(AverageRange) Cells(10, ColCount) = ColAverage Next ColCount End If Application.OnTime Now + _ TimeValue("00:00:03"), "CalcAverage" End Sub "soccerdav2003" wrote: Hey Joel, This isn't working. It's getting hung up on ColAverage. All I did was copy your code into my procedure form. "Joel" wrote: Try this code. Don't know what answer you want in A12? I put the average of the last 30 entries in row 10 for each column. Sub CalcAverage() StartRow = 12 'loop forever Do While (1) 'Get Last row LastRow = Range("A" & Rows.Count).End(xlUp).Row 'only collect data when 30 rows are reached If (LastRow - StartRow) + 1 = 30 Then 'perform averages on last 30 rows FirstRow = LastRow - 30 + 1 For ColCount = 1 To Range("AZ10").Column Set AverageRange = _ Range(Cells(FirstRow, ColCount), Cells(LastRow, ColCount)) ColAverage = WorksheetFunction.Average(AverageRange) Cells(10, ColCount) = ColAverage Next ColCount End If Application.Wait Now() + Second(10) Loop End Sub "soccerdav2003" wrote: Hi, I've been attempting to write a macro to do this and received prior feedback. First response was I did not need a macro, I can use the formula: =IF(COUNT(A10:A1010),AVERAGE(A1010:INDEX(A10:A1010 ,INDEX(LARGE((A10:A1010<"")*ROW(A10:A1010),31),0) )),0) Even though this works, I can't make heads or tails of this. When I try to break this down into smaller functions, it falls apart What is going on by multiplying ((A10:A1010<")*Row(A10:1010),31)0) Using Nested indexes? What does A1010<"" mean? What are some rules for cell references? How do I make a cell reference a variable? Can somone please dicipher this... for me? Also, what if the number of data points collected has not reached 30 yet? Will this function fail? It does not appear to be failing when I test it with just 3 values, but I dont' really understand specifically what the function is doing. __________________________________________________ __________________ The second response was a macro. Sub Math() Dim lastRow As Long Dim sh As Worksheet For Each sh In Worksheets sh.Activate With sh lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row .Cells(lastRow, "B").FormulaR1C1 _ = "=average(r[-1]c:r[-30]c)" End With Next sh End Sub __________________________________________________ __________________ There are a few things that I would like it to do differently. I want the macro to (starting from Row 12) find the last row and then average the last 30 entities, and post the result on Row 10. I only need to run the macro on a specific worksheet, not multiple sheets. I want to start the macro running once I get 30 sets of data Also, data is recorded in column's A:AZ, and I have to perform this function on every column throughout the entire spectrum. Every so often, I hit a button to copy and paste special the values to another spreadsheet, so this macro has to continue to run while I run another macro at random intervals. I would like to increase my data field from 1200 points to 12,000 points so I can run my test for anywhere from 1 hour (data collected every 3 seconds) to 10 hours __________________________________________________ __________________ Also, in the statement "=average(r[-1]c:r[-30]c)", is there a way to replace the -1 and -30 with counters to represent the row index number like "x" and "y", where "X"=50 and "y" =80. How do I get the macro to put the answer in a specific Row that I activate versus .Cells(lastRow,"A"). I want the answer to be displayed on Cells("A12") |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Searching multiple data to show desired result | Excel Discussion (Misc queries) | |||
how do I write a macro to show the find dialog box in excel 2003 | Excel Discussion (Misc queries) | |||
Advanced formula - Return result & Show Cell Reference of result | Excel Worksheet Functions | |||
How identify data of two coulams,Every cell count and show result | Excel Discussion (Misc queries) | |||
I need to find a macro to find data cut and paste to another colu. | Excel Programming |