Sub 修改数据()
Dim wsSheet1001 As Worksheet
Dim ws客户汇总 As Worksheet
Dim ws厚度汇总 As Worksheet
Dim rng As Range, cell As Range
Dim i As Long, j As Long
' 初始化相关变量和对象
Set wsSheet1001 = ThisWorkbook.Worksheets("sheet1001")
' 检查并处理“厚度汇总”和“客户汇总”工作表
On Error Resume Next
Set ws客户汇总 = ThisWorkbook.Worksheets("客户汇总")
Set ws厚度汇总 = ThisWorkbook.Worksheets("厚度汇总")
On Error GoTo 0
If ws客户汇总 Is Nothing Then
Set ws客户汇总 = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws客户汇总.Name = "客户汇总"
ws客户汇总.Cells(1, 1) = "客户名称"
ws客户汇总.Cells(1, 2) = "厚度"
ws客户汇总.Cells(1, 3) = "平方总和"
ws客户汇总.Cells(1, 4) = "立方总和"
ws客户汇总.Cells(1, 5) = "总金额"
End If
If ws厚度汇总 Is Nothing Then
Set ws厚度汇总 = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws厚度汇总.Name = "厚度汇总"
ws厚度汇总.Cells(1, 1) = "厚度"
ws厚度汇总.Cells(1, 2) = "平方总和"
ws厚度汇总.Cells(1, 3) = "立方总和"
ws厚度汇总.Cells(1, 4) = "总金额"
End If
' 清空汇总表中的旧数据
ws客户汇总.Rows("2:" & ws客户汇总.Rows.Count).ClearContents
ws厚度汇总.Rows("2:" & ws厚度汇总.Rows.Count).ClearContents
' 从“sheet1001”工作表中提取数据,根据 E 列是否为空进行筛选
Set rng = wsSheet1001.Range("E2:E" & wsSheet1001.Cells(wsSheet1001.Rows.Count, "E").End(xlUp).Row)
i = 2
For Each cell In rng
If cell.Value <> "" Then
' 汇总到“客户汇总”
ws客户汇总.Cells(i, 1).Value = wsSheet1001.Cells(cell.Row, 1).Value
ws客户汇总.Cells(i, 2).Value = wsSheet1001.Cells(cell.Row, 5).Value
ws客户汇总.Cells(i, 3).Value = wsSheet1001.Cells(cell.Row, 4).Value
ws客户汇总.Cells(i, 4).Value = wsSheet1001.Cells(cell.Row, 6).Value
ws客户汇总.Cells(i, 5).Value = wsSheet1001.Cells(cell.Row, 10).Value
' 汇总到“厚度汇总”
ws厚度汇总.Cells(i, 1).Value = wsSheet1001.Cells(cell.Row, 5).Value
ws厚度汇总.Cells(i, 2).Value = wsSheet1001.Cells(cell.Row, 4).Value
ws厚度汇总.Cells(i, 3).Value = wsSheet1001.Cells(cell.Row, 6).Value
ws厚度汇总.Cells(i, 4).Value = wsSheet1001.Cells(cell.Row, 10).Value
i = i + 1
End If
Next cell
' 重新检查汇总数据,如有问题则返回重新执行一遍
GoTo 再次检查
再次检查:
' 对“厚度汇总”的数据进行汇总计算
Call 厚度汇总计算(ws厚度汇总)
' 对“客户汇总”的数据进行汇总排序
Call 客户汇总整理(ws客户汇总)
' 清除空白行
Call 清除空白行(ws厚度汇总)
Call 清除空白行(ws客户汇总)
' 自动调整列宽
ws厚度汇总.Columns.AutoFit
ws客户汇总.Columns.AutoFit
End Sub
Sub 厚度汇总计算(ws As Worksheet)
' 对“厚度汇总”的数据,所有厚度相同的平方数、立方数、总金额进行汇总计算
Dim lastRow As Long
Dim i As Long, j As Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
For j = lastRow To i + 1 Step -1
If ws.Cells(i, 1).Value = ws.Cells(j, 1).Value Then
ws.Cells(i, 2).Value = ws.Cells(i, 2).Value + ws.Cells(j, 2).Value
ws.Cells(i, 3).Value = ws.Cells(i, 3).Value + ws.Cells(j, 3).Value
ws.Cells(i, 4).Value = ws.Cells(i, 4).Value + ws.Cells(j, 4).Value
ws.Rows(j).Delete
lastRow = lastRow - 1
End If
Next j
Next i
End Sub
Sub 客户汇总整理(ws As Worksheet)
' 对“客户汇总”的数据,先按客户名称排个序,然后按厚度排个序,同一客户相同厚度的汇总一下
Dim lastRow As Long
Dim i As Long, j As Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' 按客户名称和厚度排序
ws.Range("A2:E" & lastRow).Sort Key1:=ws.Range("A2"), Order1:=xlAscending, Key2:=ws.Range("B2"), Order2:=xlAscending, Header:=xlNo
' 同一客户的相同厚度汇总一下
For i = 2 To lastRow
For j = lastRow To i + 1 Step -1
If ws.Cells(i, 1).Value = ws.Cells(j, 1).Value And ws.Cells(i, 2).Value = ws.Cells(j, 2).Value Then
ws.Cells(i, 3).Value = ws.Cells(i, 3).Value + ws.Cells(j, 3).Value
ws.Cells(i, 4).Value = ws.Cells(i, 4).Value + ws.Cells(j, 4).Value
ws.Cells(i, 5).Value = ws.Cells(i, 5).Value + ws.Cells(j, 5).Value
ws.Rows(j).Delete
lastRow = lastRow - 1
End If
Next j
Next i
End Sub
Sub 清除空白行(ws As Worksheet)
' 清除工作表中的空白行
Dim lastRow As Long
Dim i As Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For i = lastRow To 2 Step -1
If Application.WorksheetFunction.CountA(ws.Rows(i)) = 0 Then
ws.Rows(i).Delete
End If
Next i
End Sub