2015-06-14 84 views
0

我对VBA不够熟悉,但不幸的是我必须完成它。VBA从两个时间之间的单元格中总结值

我从仓库管理系统,它看起来像,进行了摘录: enter image description here

我需要根据提取这是巨大的计算所有员工的生产力。

所以我准备xls与2个额外的选项卡。第一个选项卡包含上面的提取frm。第二标签包含计数看起来像该

enter image description here

有97排,开始和结束时间(24个小时分割为15分钟份)

第三标签包含的结果,看起来像:

enter image description here

我必须创建VBA宏来计算生产力。宏必须:

  • 总结挑选了多少产品(用RF枪扫描)并将其存储在特定的15分钟部分中。
  • 总共有多少15分钟的用户正在使用RF枪扫描产品(4 * 15分钟部件= 1小时)。例如:用户从提取物中扫描产品从03:21到03:43因此在标签计数中我应该有按照Comodity(有3个comodities:Ambient,Chill,Frozen)存储在03:15到03:30和03:30到03:45之间的情况,结果必须存储在第三个选项卡Result中。如果有人在3个不同的区域扫描产品,我应该在结果选项卡中为3个不同区域设置3个不同的总和。

正如我所说。我不熟悉VBA,但希望你能帮助我。如果没有,我将不得不这样做,通过填写由仓库成员填写到Excel中填充的纸质活动表,并自己计算什么会花费我很多时间为100人。

我会很感激任何帮助。

+1

你为什么需要VBA?如果我正确理解您的需求,简单的Excel Sumifs和Countifs函数可以为您做到这一点。 – vacip

+0

或者像python或php这样的脚本语言会很快做到你想要的。 –

+0

如果我将在休息日,其他人不得不这样做。我不知道关于公式的理解和擅长这个其他人,所以我宁愿让宏在粘贴相关日期后自动执行它。其次,我无法想象公式如何看起来像。 不幸的是只有脚本语言我可以使用VBA。我不能使用其他任何东西。 – LukeJ

回答

0

数据透视表是要走的路。这将在第一个摘要中引发你的兴趣。您需要使用Excel 2013作为第二个,才能够为15分钟的块选择DISTINCT计数。

Dim wb As Workbook 

Dim sourceWorksheet As Worksheet 
Dim summaryWorksheet As Worksheet 

Dim pvt1 As PivotTable 

Dim firstCell As Range 
Dim lastRow As Long 


Set wb = ActiveWorkbook 

Set sourceWorksheet = wb.ActiveSheet 


'Add two extra columns to calculate 15 minute blocks 
'=================================================== 

'***** SET THIS TO THE TOP-LEFT CELL OF THE DATA THAT CONTAINS "Hours" LABEL 
Set firstCell = sourceWorksheet.Range("A4") 


'Determine the last row 
lastRow = firstCell.CurrentRegion.Rows.Count + 3 

'Put column captions on new columns 
Cells(firstCell.Row, firstCell.Column + 4).Value = "Start" 
Cells(firstCell.Row, firstCell.Column + 5).Value = "End" 

'Set the formula for "Start" of 15 minute block 
Range(Cells(firstCell.Row + 1, firstCell.Column + 4), Cells(lastRow, firstCell.Column + 4)).Formula = "=FLOOR(" & Cells(firstCell.Row + 1, firstCell.Column).Address(False, False) & ",TIME(0,15,0))" 

'Set the formula for "End" of 15 minute block 
Range(Cells(firstCell.Row + 1, firstCell.Column + 5), Cells(lastRow, firstCell.Column + 5)).Formula = "=" & Cells(firstCell.Row + 1, firstCell.Column + 4).Address(False, False) & "+""00:15""" 

'Format the columns as times 
Range(Cells(firstCell.Row, firstCell.Column + 4), Cells(lastRow, firstCell.Column + 5)).NumberFormat = "hh:mm" 


'Create a pivot table for summary by 15 minute block 
'=================================================== 

Set summaryWorksheet = Sheets.Add 
summaryWorksheet.Name = "Summary" 

Set pvt1 = wb.PivotCaches.Create(SourceType:=xlDatabase, _ 
           SourceData:=sourceWorksheet.Name & "!" & firstCell.CurrentRegion.Address(ReferenceStyle:=xlR1C1), _ 
           Version:=xlPivotTableVersion15).CreatePivotTable(TableDestination:=summaryWorksheet.Name & "!R3C1", _ 
                       TableName:="PivotTable1", _ 
                       DefaultVersion:=xlPivotTableVersion15) 
With pvt1.PivotFields("Start") 
    .Orientation = xlRowField 
    .Position = 1 
End With 
With pvt1.PivotFields("End") 
    .Orientation = xlRowField 
    .Position = 2 
End With 
With pvt1.PivotFields("User") 
    .Orientation = xlRowField 
    .Position = 3 
End With 
pvt1.AddDataField pvt1.PivotFields("Cases"), "Sum of Cases", xlSum 
'Change the style to be tabular 
pvt1.RowAxisLayout xlTabularRow 
'Remove unwanted subtotals 
pvt1.PivotSelect "End[All;Total]", xlDataAndLabel, True 
pvt1.PivotFields("End").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False) 
pvt1.PivotSelect "Start[All;Total]", xlDataAndLabel, True 
pvt1.PivotFields("Start").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False) 
pvt1.PivotSelect "Start[All]", xlLabelOnly, True 
Range("A3").Select 

'Be a good memory citizen 
Set wb = Nothing 
Set sourceWorksheet = Nothing 
Set summaryWorksheet = Nothing 
Set pvt1 = Nothing 
Set pvt2 = Nothing 
Set firstCell = Nothing 
+0

:/这可能是一个问题,因为我的工作只有Excel 2007。 – LukeJ

+0

您是否可以澄清您的要求:“总共有多少15分钟的用户正在使用RF枪扫描产品”。如果他们在同一个块(3:00-3:15)中扫描3次,它会计为“1”还是“3”? – joehanna

+0

作为扫描产品总数的“1”在步骤1中计数。在步骤2中,我需要知道该操作花费了多长时间,这就是为什么我需要统计他们正在做这件事的数量为15分钟。 – LukeJ

相关问题