|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub ExtractUniqueCustomersAndSum()
Dim wsA As Worksheet, wsB As Worksheet
Dim lastRowA As Long, lastRowB As Long
Dim customer As Variant
Dim dictCustomers As Object
Dim i As Long, j As Long
Dim sumArray() As Double
Dim customerName As String
Dim month As Integer
Dim sumValue As Double
' 设置工作表
Set wsA = ThisWorkbook.Sheets("营业情况")
Set wsB = Workbooks("每日报表.xlsx").Sheets("签单总表")
' 初始化字典和数组
Set dictCustomers = CreateObject("Scripting.Dictionary")
ReDim sumArray(1 To 12, 1 To 1) ' 假设最多有12个月和1列客户名称
' 提取不重复的客户名称
lastRowA = wsA.Cells(wsA.Rows.Count, 1).End(xlUp).Row
For i = 7 To lastRowA ' 假设第一行是标题行
customerName = wsA.Cells(i, 1).Value
If Not dictCustomers.Exists(customerName) Then
dictCustomers.Add customerName, CreateMonthSumDict() ' 添加客户名称并初始化月份求和字典
End If
' 更新月份求和字典
UpdateMonthSumDict(dictCustomers(customerName), wsA.Cells(i, 2).Value, wsA.Cells(i, 3).Value)
Next i
' 将不重复的客户名称复制到工作表B
lastRowB = wsB.Cells(wsB.Rows.Count, 1).End(xlUp).Row + 1
For Each customerName In dictCustomers.Keys
wsB.Cells(lastRowB, 1).Value = customerName
lastRowB = lastRowB + 1
Next customerName
' 将求和结果写回到工作表B
Dim customer As Variant
For Each customerName In dictCustomers.Keys
customer = dictCustomers(customerName)
lastRowB = wsB.Cells(wsB.Rows.Count, 1).End(xlUp).Row + 1
wsB.Cells(lastRowB, 1).Value = customerName
For month = 1 To 12
If customer.Exists(month) Then
wsB.Cells(lastRowB, month + 1).Value = customer(month)
Else
wsB.Cells(lastRowB, month + 1).Value = 0
End If
Next month
Next customerName
End Sub
' 创建一个新的月份求和字典
Function CreateMonthSumDict() As Object
Set CreateMonthSumDict = CreateObject("Scripting.Dictionary")
Dim i As Integer
For i = 1 To 12
CreateMonthSumDict.Add i, 0 ' 初始化每个月份的求和为0
Next i
End Function
' 更新月份求和字典
Sub UpdateMonthSumDict(ByRef monthSumDict As Object, ByVal month As Integer, ByVal sumValue As Double)
If monthSumDict.Exists(month) Then
monthSumDict(month) = monthSumDict(month) + sumValue
Else
monthSumDict.Add month, sumValue
End If
End Sub
请问一下大佬们 标红的地方显示语法错误 要怎么改才能运行起来呢/
|
|