在我的预测中,我一次在Excel中预测了多个实体。对于某些实体,由于数据结构的原因,我必须多次迭代计算PL和BS的某些部分。
当我完成预测后,我将结果转储到FC_OUTPUT表中,并且当实体(列D),GREN(列H)和IC关系(列I)相同时,我想将数据分组/汇总在一起。该代码将循环遍历各行,如果找到匹配项,则必须将各行加在一起并删除添加的行。 您可以在下面找到我的代码。 Michael Murphy(https://stackoverflow.com/users/8826022/michael-murphy)使我成为了很棒的代码,以克服以前代码中的大多数缺陷。 不幸的是,我仍然在两个关键方面挣扎: 1.对于ICCol排序,我收到运行时错误'1004':对象'_Global'的方法'Range'失败。为什么对其他两列进行排序可以很好地工作。 2.从第12列添加行值直到第96列不起作用。 我对此有点不满意,非常感谢所有帮助。
Sub itest()
Dim EntityCol As Long, GRENCol As Long, ICCol As Long, ValueCol As Long, i As Long
Dim firstrow As Long, lastrow As Long, rngData As Range
Worksheets("FC_OUTPUT").Activate
Application.ScreenUpdating = False
EntityCol = 4 ' column D
GRENCol = 8
ICCol = 9
ValueCol = 12 ' column L
firstrow = 7
lastrow = Cells(Rows.Count, EntityCol).End(xlUp).Row
With ActiveSheet.Sort
.SortFields.Add Key:=Range(Cells(firstrow, EntityCol)), Order:=xlAscending
.SortFields.Add Key:=Range(Cells(firstrow, ICCol)), Order:=xlAscending
.SortFields.Add Key:=Range(Cells(firstrow, GRENCol)), Order:=xlAscending
.SetRange Range(Cells(firstrow, 1), Cells(lastrow, 96))
.Header = xlNo
.Apply
End With
Set rngData = Range(Cells(firstrow, 1), Cells(lastrow, 96)) ' this line should be adjusted but you'll need to also adjust firstrow and lastrow
With rngData
' Here I'll start a loop for every row going from the end to the beginning, to prevent issues when removing rows
For i = lastrow To firstrow Step -1
' Here I'll use the If statement to check if the values are the same as the previous row
If .Cells(i, EntityCol) = .Cells(i - 1, EntityCol) And _
.Cells(i, GRENCol) = .Cells(i - 1, GRENCol) And _
.Cells(i, ICCol) = .Cells(i - 1, ICCol) Then
' This is where you'll do your addition and delete
.Cells(i - 1, ValueCol).Value2 = .Cells(i - 1, ValueCol) + .Cells(i, ValueCol)
.Rows(i).Delete
End If
Next i
End With
End Sub
答案 0 :(得分:0)
您需要弄乱范围和仔细检查列号以确保它适合您的数据,但这是我如何执行任务的一个示例。也许这将有助于您朝更好的方向发展。假设数据已正确排序,并且严格将每一行与之前的行进行比较:
Sub itest()
Dim EntityCol As Long, GRENCol As Long, ICCol As Long, ValueCol As Long, i As Long
Dim firstrow As Long, lastrow As Long, rngData As Range
EntityCol = 4 ' column D
GRENCol = 8
ICCol = 9
ValueCol = 12 ' column L
firstrow = 7
Set rngData = Cells ' this line should be adjusted but you'll need to also adjust firstrow and lastrow
' To find the last row, I'm assuming that your Entity column has values all the way to the end of your data
lastrow = Cells(Rows.Count, EntityCol).End(xlUp).Row
With rngData
' Here I'll start a loop for every row going from the end to the beginning, to prevent issues when removing rows
For i = lastrow To firstrow Step -1
' Here I'll use the If statement to check if the values are the same as the previous row
If .Cells(i, EntityCol) = .Cells(i - 1, EntityCol) And _
.Cells(i, GRENCol) = .Cells(i - 1, GRENCol) And _
.Cells(i, ICCol) = .Cells(i - 1, ICCol) Then
' This is where you'll do your addition and delete
.Cells(i - 1, ValueCol).Value2 = .Cells(i - 1, ValueCol) + .Cells(i, ValueCol)
.Rows(i).Delete
End If
Next i
End With
End Sub