|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 shadowmage 于 2024-7-11 06:30 编辑
如题,该程序要求如下:
1、初始化相关变量和对象,包括工作表引用、字典对象等。
2、检查并处理“厚度汇总”和“客户汇总”工作表,如果不存在则新建并设置表头。
3、从“sheet1001”工作表中提取数据,根据 E 列是否为空进行筛选。
4、“sheet1001”sheet的A列,汇总到“客户汇总”sheet的“客户名称”列。
5、“sheet1001”sheet的E列,汇总到“客户汇总”sheet的“厚度”列。
6、“sheet1001”sheet的D列,汇总到“客户汇总”sheet的“平方总和”列。
7、“sheet1001”sheet的F列,汇总到“客户汇总”sheet的“立方总和”列。
8、“sheet1001”sheet的J列,汇总到“客户汇总”sheet的“总金额”列。
9、“sheet1001”sheet的E列,汇总到“厚度汇总”sheet的“厚度”列。
10、“发货台帐”sheet的D列,汇总到“厚度汇总”sheet的“平方总和”列。
11、“sheet1001”sheet的F列,汇总到“厚度汇总”sheet的“立方总和”列。
12、“发货台帐”sheet的J列,汇总到“厚度汇总”sheet的“总金额”列。
13、检查一下厚度汇总数据跟客户汇总数据,跟“sheet1001”工作表中相应数据是否对应,不对的重新进行1-9步骤。
14、对于筛选出的数据,分别按照厚度和客户名称-厚度的组合作为键,使用字典存储平方、立方和发货金额的汇总数据。
15、将厚度汇总数据和客户汇总数据分别写入对应的工作表。
16、清除“厚度汇总”工作表中A列为空的行。
17、清除“客户汇总”工作表中B列为空的行。
18、清除“厚度汇总”和“客户汇总”工作表中的空白行。
19、自动调整“厚度汇总”和“客户汇总”工作表的列宽。
20、释放字典对象占用的资源。
目前让豆包帮忙编的,有点框架了,但是需要改进,而我试了很多次,发现依靠豆包,改进不能。
Sub ExcelAutomation()
Dim wb As Workbook
Dim wsShipping As Worksheet
Dim wsThicknessSummary As Worksheet
Dim wsCustomerSummary As Worksheet
Dim dictThickness As Object
Dim dictCustomerThickness As Object
Dim row As Long
Dim lastRow As Long
Dim found As Boolean
'初始化变量和对象
Set wb = ThisWorkbook
Set wsShipping = wb.Sheets("sheet1001")
On Error Resume Next
Set wsThicknessSummary = wb.Sheets("厚度汇总")
If wsThicknessSummary Is Nothing Then
Set wsThicknessSummary = wb.Sheets.Add
wsThicknessSummary.Name = "厚度汇总"
wsThicknessSummary.Cells(1, 1).Value = "厚度"
wsThicknessSummary.Cells(1, 2).Value = "平方总和"
wsThicknessSummary.Cells(1, 3).Value = "立方总和"
wsThicknessSummary.Cells(1, 4).Value = "总金额"
End If
On Error Resume Next
Set wsCustomerSummary = wb.Sheets("客户汇总")
If wsCustomerSummary Is Nothing Then
Set wsCustomerSummary = wb.Sheets.Add
wsCustomerSummary.Name = "客户汇总"
wsCustomerSummary.Cells(1, 1).Value = "客户名称"
wsCustomerSummary.Cells(1, 2).Value = "厚度"
wsCustomerSummary.Cells(1, 3).Value = "平方总和"
wsCustomerSummary.Cells(1, 4).Value = "立方总和"
wsCustomerSummary.Cells(1, 5).Value = "总金额"
End If
Set dictThickness = CreateObject("Scripting.Dictionary")
Set dictCustomerThickness = CreateObject("Scripting.Dictionary")
'提取并汇总数据
lastRow = wsShipping.Cells(wsShipping.Rows.Count, "A").End(xlUp).Row
For row = 2 To lastRow
If Not IsEmpty(wsShipping.Cells(row, "E").Value) Then
'客户汇总
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "A").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "B").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "E").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "C").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "D").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "D").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "F").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "E").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "J").Value
'厚度汇总
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "E").Value
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "B").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "D").Value
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "C").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "F").Value
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "D").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "J").Value
'字典汇总
If Not dictThickness.Exists(wsShipping.Cells(row, "E").Value) Then
dictThickness.Add wsShipping.Cells(row, "E").Value, Array(0, 0, 0)
End If
If Not dictCustomerThickness.Exists(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value) Then
dictCustomerThickness.Add wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value, Array(0, 0, 0)
End If
Dim squareSum As Double
Dim cubeSum As Double
Dim amountSum As Double
squareSum = dictThickness(wsShipping.Cells(row, "E").Value)(0) + wsShipping.Cells(row, "D").Value
cubeSum = dictThickness(wsShipping.Cells(row, "E").Value)(1) + wsShipping.Cells(row, "F").Value
amountSum = dictThickness(wsShipping.Cells(row, "E").Value)(2) + wsShipping.Cells(row, "J").Value
dictThickness(wsShipping.Cells(row, "E").Value) = Array(squareSum, cubeSum, amountSum)
squareSum = dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value)(0) + wsShipping.Cells(row, "D").Value
cubeSum = dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value)(1) + wsShipping.Cells(row, "F").Value
amountSum = dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value)(2) + wsShipping.Cells(row, "J").Value
dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value) = Array(squareSum, cubeSum, amountSum)
End If
Next row
'检查数据是否对应
found = True
For row = 2 To wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "A").End(xlUp).Row
If Not dictThickness.Exists(wsThicknessSummary.Cells(row, 1).Value) Then
found = False
Exit For
Else
Dim expectedSquareSum As Double
Dim expectedCubeSum As Double
Dim expectedAmountSum As Double
expectedSquareSum = dictThickness(wsThicknessSummary.Cells(row, 1).Value)(0)
expectedCubeSum = dictThickness(wsThicknessSummary.Cells(row, 1).Value)(1)
expectedAmountSum = dictThickness(wsThicknessSummary.Cells(row, 1).Value)(2)
If wsThicknessSummary.Cells(row, 2).Value <> expectedSquareSum Or _
wsThicknessSummary.Cells(row, 3).Value <> expectedCubeSum Or _
wsThicknessSummary.Cells(row, 4).Value <> expectedAmountSum Then
found = False
Exit For
End If
End If
Next row
If Not found Then
wsThicknessSummary.Rows("2:" & wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "A").End(xlUp).Row).ClearContents
wsCustomerSummary.Rows("2:" & wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "A").End(xlUp).Row).ClearContents
For row = 2 To lastRow
If Not IsEmpty(wsShipping.Cells(row, "E").Value) Then
'客户汇总
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "A").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "B").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "E").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "C").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "D").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "D").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "F").Value
wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "E").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "J").Value
'厚度汇总
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "E").Value
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "B").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "D").Value
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "C").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "F").Value
wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "D").End(xlUp).Offset(1, 0).Value = wsShipping.Cells(row, "J").Value
'字典汇总
If Not dictThickness.Exists(wsShipping.Cells(row, "E").Value) Then
dictThickness.Add wsShipping.Cells(row, "E").Value, Array(0, 0, 0)
End If
If Not dictCustomerThickness.Exists(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value) Then
dictCustomerThickness.Add wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value, Array(0, 0, 0)
End If
Dim squareSum As Double
Dim cubeSum As Double
Dim amountSum As Double
squareSum = dictThickness(wsShipping.Cells(row, "E").Value)(0) + wsShipping.Cells(row, "D").Value
cubeSum = dictThickness(wsShipping.Cells(row, "E").Value)(1) + wsShipping.Cells(row, "F").Value
amountSum = dictThickness(wsShipping.Cells(row, "E").Value)(2) + wsShipping.Cells(row, "J").Value
dictThickness(wsShipping.Cells(row, "E").Value) = Array(squareSum, cubeSum, amountSum)
squareSum = dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value)(0) + wsShipping.Cells(row, "D").Value
cubeSum = dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value)(1) + wsShipping.Cells(row, "F").Value
amountSum = dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value)(2) + wsShipping.Cells(row, "J").Value
dictCustomerThickness(wsShipping.Cells(row, "A").Value & "-" & wsShipping.Cells(row, "E").Value) = Array(squareSum, cubeSum, amountSum)
End If
Next row
End If
'清除特定空行
Dim delRow As Long
delRow = wsThicknessSummary.Rows.Count
While wsThicknessSummary.Cells(delRow, 1).Value = ""
wsThicknessSummary.Rows(delRow).Delete
delRow = delRow - 1
Wend
delRow = wsCustomerSummary.Rows.Count
While wsCustomerSummary.Cells(delRow, 2).Value = ""
wsCustomerSummary.Rows(delRow).Delete
delRow = delRow - 1
Wend
'清除空白行
wsThicknessSummary.Range(wsThicknessSummary.Cells(wsThicknessSummary.Rows.Count, "A").End(xlUp).Row + 1, wsThicknessSummary.Rows.Count).EntireRow.Delete
wsCustomerSummary.Range(wsCustomerSummary.Cells(wsCustomerSummary.Rows.Count, "A").End(xlUp).Row + 1, wsCustomerSummary.Rows.Count).EntireRow.Delete
'自动调整列宽
wsThicknessSummary.Columns.AutoFit
wsCustomerSummary.Columns.AutoFit
'释放字典对象
Set dictThickness = Nothing
Set dictCustomerThickness = Nothing
End Sub
目前是“Dim squareSum As Double”这句出错,当前范围内的声明重复。让豆包修改吧,豆包就崩溃了
想求助FishC,看看能否根据我前面需求,把这个VBA代码给改改好。 |
|