带有循环的产品数组
我正在构build一个电子表格,并试图获得每位员工每天的生产力总和。 方程很容易 – >(活动1 *周期时间1)+(活动2 *周期时间2)…..我有x列的数量(他们每次都改变)和Y的行数量(每天增长)。 数据示例如下所示:
date| 名称| Act 1 | CT 1 | Act 2 | CT 2 | 生产率
1/15/2016 | 乔纳森| 20 | 2 | 10 | 1.5 | …..
1/14/2016 | 比利| 19 | 2 | 10 | 1.5 | …..
我该如何自动化第(act 1 * Ct1)+(Act 2 * Ct2)到第n个方程,保持一切dynamic?
我觉得我需要将活动存储在一个数组中,并将CycleTime存储在另一个数组中,然后将这两个数组相乘。 我不知道如何做到这一点,因为我对数组非常新鲜,在VBA上相当成熟。 我一直在想其他的方法来做到这一点,但一直没有能够拿出任何东西。
有人能帮我吗? 充分的解释也将不胜感激。 🙂
Sub InsertColumnsAndFormulasUntilEndOfProductivityTable_MakeProductivityNumbers() With Sheet6 Set EmployeesRange = .Range("A1", .Range("B1").End(xlDown)) End With With Sheet1 Set ActivityRange = .Range("A1", .Range("B1").End(xlDown)) End With 'insert column (For i = 1...) and then vlookup (FormulaRange1.Formula...) With Sheet4 y = .Cells(.Rows.Count, 1).End(xlUp).Row x = (.Cells(1, .Columns.Count).End(xlToLeft).Column) * 2 Startrow = 2 StartColumn = 2 For i = 1 + StartColumn To (x + 1) Step 2 .Columns(i).EntireColumn.Insert Set FormulaRange1 = .Range(.Cells(Startrow, i), .Cells(y, i)) If i = 3 Then 'insert title for usernames and then vlookup Cells(Startrow - 1, i).Value = Cells(Startrow - 1, i - 1).Value & "'s Team" FormulaRange1.FormulaR1C1 = "=VLookup(R[0]C[-1],'" & EmployeesRange.Parent.Name & "'!" & EmployeesRange.Address(1, 1, xlR1C1) & ", 2, False)" ElseIf i <= x Then 'insert title for activities and then vlookup lock row 1 Cells(Startrow - 1, i).Value = Cells(Startrow - 1, i - 1).Value & " Cycle Time" FormulaRange1.FormulaR1C1 = "=VLookup(R1C[-1],'" & ActivityRange.Parent.Name & "'!" & ActivityRange.Address(1, 1, xlR1C1) & ", 2, False)" Else 'Sum totals of productivity per person per day Cells(Startrow - 1, i - 1).Value = "Totals" 'THIS IS WHERE THE PRODUCTIVITY EQUATION/LOOP NEEDS TO GO!... End If Next End With End Sub
我的build议是创build一个用户定义的函数(UDF),你可以在数据的末尾input。 下面的函数定义单个date/用户的数据范围,即单行。 快速简单。
我的testing数据是这样设置的:
在“生产力”列中,每个单元格都有以下公式:
=CalcProductivity(C2:J2)
UDF在VBA模块中定义如下:
Option Explicit Public Function CalcProductivity(dataRange As Range) As Double '--- input range is 'n' pairs of activity,cycle data. ' productivity is calculated by the sum of all activity * cycle pairs Dim dataArray As Variant Dim i As Long, j As Long Dim runningSum As Double dataArray = dataRange 'copy to memory array for speed runningSum = 0# For i = 1 To UBound(dataArray, 2) Step 2 runningSum = runningSum + (dataArray(1, i) * dataArray(1, i + 1)) Next i CalcProductivity = runningSum End Function
Sub InsertColumnsAndFormulasUntilEndOfProductivityTable_MakeProductivityNumbers() With Sheet6 Set EmployeesRange = .Range("A1", .Range("B1").End(xlDown)) End With With Sheet1 Set ActivityRange = .Range("A1", .Range("B1").End(xlDown)) End With 'insert column (For i = 1...) and then vlookup (FormulaRange1.Formula...) With Sheet4 y = .Cells(.Rows.Count, 1).End(xlUp).Row Dim j As Long x = (.Cells(1, .Columns.Count).End(xlToLeft).Column) * 2 Startrow = 2 StartColumn = 2 j = ActiveCell.Row For i = 1 + StartColumn To (x + 1) Step 2 .Columns(i).EntireColumn.Insert Set FormulaRange1 = .Range(.Cells(Startrow, i), .Cells(y, i)) If i = 3 Then 'insert title for usernames and then vlookup Cells(Startrow - 1, i).Value = Cells(Startrow - 1, i - 1).Value & "'s Team" FormulaRange1.FormulaR1C1 = "=VLookup(R[0]C[-1],'" & EmployeesRange.Parent.Name & "'!" & EmployeesRange.Address(1, 1, xlR1C1) & ", 2, False)" ElseIf i <= x Then 'insert title for activities and then vlookup lock row 1 Cells(Startrow - 1, i).Value = Cells(Startrow - 1, i - 1).Value & " Cycle Time" FormulaRange1.FormulaR1C1 = "=VLookup(R1C[-1],'" & ActivityRange.Parent.Name & "'!" & ActivityRange.Address(1, 1, xlR1C1) & ", 2, False)" Else 'Sum totals of productivity per person per day 'the loop is for each row, to ensure a productivity number per person Cells(Startrow - 1, i - 1).Value = "Totals" For j = 2 To y Set DataRange2 = .Range(.Cells(j, StartColumn + 2), .Cells(j, i)) Cells(j, i - 1).Value = CalcProductivity(DataRange2) Next End If Next End With End Sub
'函数来查找生产力数字(row1 * column1)+(row2 * column2)…
Public Function CalcProductivity(dataRange As Range) As Double '--- input range is 'n' pairs of activity,cycle data. ' productivity is calculated by the sum of all activity * cycle pairs Dim dataArray As Variant Dim i As Long, t As Long Dim runningSum As Double dataArray = dataRange 'copy to memory array for speed runningSum = 0# For i = 1 To UBound(dataArray, 2) Step 2 runningSum = runningSum + (dataArray(1, i) * dataArray(1, i + 1)) Next CalcProductivity = runningSum End Function