ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 161|回复: 0

[求助] 语法错误要怎么破

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-4-1 10:26 | 显示全部楼层 |阅读模式
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
请问一下大佬们 标红的地方显示语法错误 要怎么改才能运行起来呢/

help.zip

157.25 KB, 下载次数: 0

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-28 03:29 , Processed in 0.036464 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表