• excel中按多列进行匹配并对数量进行累加


    公司的生产计划是按订单下发,但不同订单的不同产品中可能有用到相同的配件,按单1对1时,对计算机十分友好,但对于在配件库检料的工人来说就比较麻烦,上百条产品里可能会有多条都是相同的产品,首先考虑的办法是把数据进行了排序,让相同代号、材质、名称的数据挨在一起,但这样也并不是最优的,然后考虑的办法是把所有代号+材质+名称+领料人相同的数据的请领数量累加到一起,使其只有一条。下面是生产系统导出到excel中的数据,其中黄色底纹的就是相同数据:

    代码就不细说了,做个流程图出来:

    下面是代码,由于vba不能像javascript那样处理JSON数据,所以在用数组处理时比较麻烦:

    1. Option Explicit
    2. Private infos() As String
    3. '汇总
    4. Sub myTotal()
    5. Dim totalRowNumber As Integer
    6. Dim i As Integer
    7. Dim j As Integer
    8. Dim arrCount As Integer '数组数据位置
    9. Dim id As String
    10. '数组初始位置
    11. arrCount = 2
    12. '获得总行数
    13. totalRowNumber = Sheets(1).[a1].End(xlDown).Row
    14. '调整数组大小(考虑到避免多次调整数组大小,所以直接定义一个跟数据行一样多的数组)
    15. ReDim infos(2 To totalRowNumber, 1 To 13)
    16. For i = 2 To totalRowNumber
    17. id = Sheets(1).Cells(i, 5) & "|" & Sheets(1).Cells(i, 6) & "|" & Sheets(1).Cells(i, 7) & "|" & Sheets(1).Cells(i, 8)
    18. If isExist(id) Then
    19. '判断当前行是否已存在数组中,如果存在则请领数量累加
    20. addRequireNumber id, Sheets(1).Cells(i, 9), Sheets(1).Cells(i, 2)
    21. Else
    22. '如果不存在则加入到数组中
    23. For j = 1 To 13
    24. infos(arrCount, j) = Sheets(1).Cells(i, j)
    25. Next
    26. '数组条数+1
    27. arrCount = arrCount + 1
    28. End If
    29. Next
    30. '清除区域数据
    31. Sheets(1).Range(totalRowNumber + 1 & ":65535").ClearContents
    32. '输出数组到表格中
    33. For i = 2 To UBound(infos)
    34. '如果infos(i,1)没有内容,说明后面都是空的行,所以就结束函数了
    35. If Len(infos(i, 1)) = 0 Then Exit Sub
    36. For j = 1 To 13
    37. Sheets(1).Cells(totalRowNumber + i, j) = infos(i, j)
    38. Next
    39. Next
    40. End Sub
    41. '判断数组是否存在
    42. Private Function isExist(ByVal name As String) As Boolean
    43. Dim i As Integer
    44. Dim id As String
    45. For i = 2 To UBound(infos)
    46. id = infos(i, 5) & "|" & infos(i, 6) & "|" & infos(i, 7) & "|" & infos(i, 8)
    47. If name = id Then
    48. isExist = True
    49. Exit Function
    50. End If
    51. Next
    52. isExist = False
    53. End Function
    54. '数量累加
    55. Private Sub addRequireNumber(ByVal name As String, ByVal requireNumber As Long, ByVal order As String)
    56. Dim i As Integer
    57. Dim id As String
    58. Dim subOrder As String
    59. '去掉订单的年号
    60. subOrder = Mid(order, InStr(1, order, "-") + 1, Len(order))
    61. For i = 2 To UBound(infos)
    62. id = infos(i, 5) & "|" & infos(i, 6) & "|" & infos(i, 7) & "|" & infos(i, 8)
    63. If name = id Then
    64. infos(i, 9) = Str(Int(infos(i, 9)) + Int(requireNumber))
    65. '如果订单号中没有包含相同的字符才添加
    66. If InStr(1, infos(i, 2), subOrder) = 0 Then
    67. infos(i, 2) = infos(i, 2) & "/" & subOrder
    68. End If
    69. Exit Sub
    70. End If
    71. Next
    72. End Sub

  • 相关阅读:
    SpringBoot安全漏洞SQL注入和XSS攻击简介说明
    互换性测量与技术——偏差与公差的计算,公差图的绘制,配合与公差等级的选择方法
    数据结构由中序序列和后序序列构造二叉树
    tonybot 人形机器人 查看端口并对应端口 0701
    通义灵码牵手阿里云函数计算 FC ,打造智能编码新体验
    Linux入门教程:P13->磁盘管理类
    【Bug】Unable to make field private final int java.time.LocalDate.year accessible
    SpringBoot SpringBoot 开发实用篇 4 数据层解决方案 4.7 SpringBoot 操作 Redis 客户端实现技术切换【jedis】
    Python利器:os与chardet读取多编码文件
    什么是机器学习中的正则化?
  • 原文地址:https://blog.csdn.net/wudechun/article/details/139702675