VBA - Average Cycle times macro

Kenn

Board Regular
Joined
Sep 23, 2009
Messages
195
Hi again. I need a little help with a macro & would appreciate any suggestions.
In workbook 01, Column A consists of a very long list of part numbers, some of which may repeat. Column B contains the cycle times for processing each part. I would like to have a macro in Workbook 02 to search Workbook 01 to find the shortest cycle time, the longest cycle time & the average cycle time for each part.

Thank you
Kenn
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Some data as example is helpful to understand your need.

Maybe:

Code:
=MIN(B:B)
=MAX(B:B)
=AVERAGE(B:B)

Work for you.
 
Upvote 0
A pivot table created by a macro will be easiest to execute, but what version of Excel are you using?
 
Upvote 0
Thanks p45cal,

I would prefer to do this with VB. I have tried to record a macro to create a pivot table, but when you go to another workbook the macro doesn't record anything.

Regards
Kenn
 
Upvote 0
I would prefer to do this with VB. I have tried to record a macro to create a pivot table, but when you go to another workbook the macro doesn't record anything.
A macro is VB(A), you just have to make adjustments.
But you haven't answered my question:
what version of Excel are you using?

For example, the following macro should, with a few provisos, work in Excel 2003 (and possibly 2010):
Code:
Sub blah()
With Workbooks("[COLOR=#ff0000]Workbook01.xls[/COLOR]").Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
Set SD = Intersect(.Range("A1").CurrentRegion, .Range("A:B"))
End With
Set PC = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=SD)
Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PC, TableDestination:=Range("A3"))
With PT
  .AddFields RowFields:=Array(SD.Cells(1).Value, "Data")
For i = 1 To 3: .PivotFields(SD.Cells(1, 2).Value).Orientation = xlDataField: Next i
  .DataPivotField.Orientation = xlColumnField
  With .DataFields(1)
    .Caption = "Min"
    .Function = xlMin
    .NumberFormat = "0.00"
  End With
  With .DataFields(2)
    .Caption = "Max"
    .Function = xlMax
    .NumberFormat = "0.00"
  End With
  With .DataFields(3)
    .Caption = "Average"
    .Function = xlAverage
    .NumberFormat = "0.00"
  End With
  .ColumnGrand = False
  .RowGrand = False
End With
End Sub

The provisos are:
1. There's a workbook called Workbook01.xls and it's open. If not adjust the name of the workbook in the code.
2. The source data is on a sheet called Sheet1, if not adjust the sheet name in the macro.
3. The sourcedata starts in A1 of the above sheet, with headers in row 1. If not, we've some tweaking to do.
4. The code adds a pivot table to the active sheet of the active workbook at the time the code is run, so make sure where you want the results is active at the time you run the code, and it would be good if the sheet is empty at the start.
 
Last edited:
Upvote 0
Thank you P45cal.

I was able to get it to work by modifying your code.

Thank you
Kenn.
 
Upvote 0

Forum statistics

Threads
1,203,171
Messages
6,053,871
Members
444,691
Latest member
Breizze1313

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