我有一张excel表格,其中包含跨越多行的单个人的数据:
e.g。
Name Speed Work Done
John Doe 10
knob twisting 2
lever pulling 15
Jane Doe 14
knob twisting 12
Joe Doe 5
lever pulling 3
我想仅为每个人完成总工作,以便得到类似以下内容:
Name Total Work Done
John Doe 17
Jane Doe 12
Joe Doe 3
试图对价值进行转置和分割,但我还没有弄清楚究竟要拆分什么。
我已经调查了一些VBA迭代行并总结了以下
中完成的单个总工作量Sub Button3_Click()
Dim jobs As Variant
jobs = Array("knob twisting", "lever pulling")
Dim sizeOfJobs As Integer
sizeOfJobs = UBound(jobs) - LBound(jobs) + 1
Dim sh As Worksheet
Set sh = ActiveSheet
Dim totalRows As Integer
totalRows = sh.UsedRange.Rows.Count + sh.UsedRange.row - 1
Dim userJobs() As Long
ReDim userJobs(totalRows)
Dim row As Range
Dim userRow As Integer
For Each row In sh.UsedRange.Rows
If sh.Cells(row.row, 0) <> "Name" Then
If IsError(Application.Match(sh.Cells(row.row, 0).Value, moves, 0)) Then
userRow = row
Else
userMoves(userRow) = userMoves(userRow) + sh.Cells(row.row, 4).Value
End If
End If
Next row
Dim element As Variant
For Each element In userMoves
Debug.Print element
Next element
End Sub
我一直在接受&#34;方法&#39; _Default&#39;对象&#39;范围&#39;失败&#34;我不确定这意味着什么
答案 0 :(得分:0)
在名称旁边再添加一列(例如“活动”),然后将所有活动移到那里。要获得Total,您可以使用函数Sum If。
Name Activity type Speed Work Done
John Doe knob twisting 10 2
John Doe lever pulling 15
Jane Doe knob twisting 14 12
Joe Doe lever pulling 5 3
Total Total work done
John Doe 17 -> =SUMIF(A2:A5,A9,E2:E5)
Jane Doe 12 -> =SUMIF(A2:A5,A10,E2:E5)
Joe Doe 3 -> =SUMIF(A2:A5,A11,E2:E5)
答案 1 :(得分:0)
找出代码的问题,单元格从1开始,而数组从0开始
Sub Button3_Click()
Dim jobs As Variant
jobs = Array("knob twisting", "lever pulling")
Dim sizeOfJobs As Integer
sizeOfJobs = UBound(jobs) - LBound(jobs) + 1
Dim sh As Worksheet
Set sh = ActiveSheet
Dim totalRows As Integer
totalRows = sh.UsedRange.Rows.Count + sh.UsedRange.row - 1
Dim userJobs() As Long
ReDim userJobs(totalRows)
Dim row As Range
Dim userRow As Integer
For Each row In sh.UsedRange.Rows
If sh.Cells(row.row, 1).Value <> "Name" Then
If IsError(Application.Match(sh.Cells(row.row, 1).Value, jobs, 0)) Then
userRow = row.row
Else
userJobs(userRow - 1) = userJobs(userRow - 1) + sh.Cells(row.row, 9).Value
userJobs(row.row) = 0
End If
End If
Next row
Dim job As Variant
Dim rowNum As Integer
rowNum = 1
For Each job In userJobs
If job <> 0 Then
sh.Cells(rowNum, 10).Value = job
End If
rowNum = rowNum + 1
Next job
End Sub