ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 发出和收回数量排序并计算结果.谢谢

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-8 11:39 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 38180203 于 2024-5-8 11:42 编辑

QQ五笔截图未命名.png
软件是WPS
同一张表,,把发出和收回的分别按名称来排序,,并计算出结果..谢谢
如果匹配不上的就往后面排
高手们有没有其他更好的方法吗,谢谢







样式.rar

12.59 KB, 下载次数: 5

TA的精华主题

TA的得分主题

发表于 2024-5-8 13:31 | 显示全部楼层
结果覆盖原表,请先备份原表。
image.png

样式.rar

18.2 KB, 下载次数: 7

TA的精华主题

TA的得分主题

发表于 2024-5-8 13:51 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. Option Explicit

  2. Sub test()
  3.     Const tbLeftColNum% = 9
  4.     Const tbRightColNum% = 10
  5.     Dim arr(), maxRowC%, maxRowL%, i&, m&, n&, leftQty&, rightQty&
  6.     Dim dLeft As Object, dRight As Object, product
  7.    
  8.     Set dLeft = CreateObject("scripting.dictionary")
  9.     Set dRight = CreateObject("scripting.dictionary")
  10.    
  11.     With Sheets("原表")
  12.         maxRowC = .Cells(Rows.Count, "C").End(xlUp).Row
  13.         maxRowL = .Cells(Rows.Count, "L").End(xlUp).Row
  14.         
  15.         For i = 3 To maxRowC
  16.             product = .Cells(i, "C")
  17.             If Not dLeft.exists(product) Then
  18.                 Set dLeft(product) = New Collection
  19.                 dLeft(product).Add .Cells(i, "A").Resize(1, tbLeftColNum).Value
  20.             Else
  21.                 dLeft(product).Add .Cells(i, "A").Resize(1, tbLeftColNum).Value
  22.             End If
  23.         Next
  24.         
  25.         For i = 3 To maxRowL
  26.             product = .Cells(i, "L")
  27.             If Not dRight.exists(product) Then
  28.                 Set dRight(product) = New Collection
  29.                 dRight(product).Add .Cells(i, "J").Resize(1, tbRightColNum).Value
  30.             Else
  31.                 dRight(product).Add .Cells(i, "J").Resize(1, tbRightColNum).Value
  32.             End If
  33.         Next
  34.     End With
  35.    
  36.     With Sheets("需要的结果")
  37.         .UsedRange.Offset(2).Clear
  38.         m = 3: n = 3
  39.         For Each product In dLeft.keys
  40.             If dRight.exists(product) Then
  41.                 leftQty = 0: rightQty = 0
  42.                
  43.                 For i = 1 To dLeft(product).Count
  44.                     .Cells(m, "A").Resize(1, tbLeftColNum) = dLeft(product)(i)
  45.                     leftQty = leftQty + dLeft(product)(i)(1, 6)
  46.                     m = m + 1
  47.                 Next
  48.                 dLeft.Remove product
  49.                
  50.                 If dRight.exists(product) Then
  51.                     For i = 1 To dRight(product).Count
  52.                         .Cells(n, "J").Resize(1, tbRightColNum) = dRight(product)(i)
  53.                         rightQty = rightQty + dRight(product)(i)(1, 4)
  54.                         n = n + 1
  55.                     Next
  56.                     dRight.Remove product
  57.                 End If
  58.                
  59.                 m = IIf(m > n, m, n) + 1: n = m
  60.                 With .Cells(n - 1, "R")
  61.                     .Value = rightQty - leftQty
  62.                     .Font.Bold = True
  63.                     .Interior.ColorIndex = 8
  64.                 End With
  65.             End If
  66.         Next
  67.         
  68.         For Each product In dLeft.keys
  69.             leftQty = 0
  70.             For i = 1 To dLeft(product).Count
  71.                 .Cells(n, "A").Resize(1, tbLeftColNum) = dLeft(product)(i)
  72.                 leftQty = leftQty + dLeft(product)(i)(1, 6)
  73.                 n = n + 1
  74.             Next
  75.             With .Cells(n, "R")
  76.                 .Value = -leftQty
  77.                 .Font.Bold = True
  78.                 .Interior.ColorIndex = 8
  79.             End With
  80.             n = n + 1
  81.         Next
  82.         
  83.         For Each product In dRight.keys
  84.             rightQty = 0
  85.             For i = 1 To dRight(product).Count
  86.                 .Cells(n, "J").Resize(1, tbRightColNum) = dRight(product)(i)
  87.                 rightQty = rightQty + dRight(product)(i)(1, 4)
  88.                 n = n + 1
  89.             Next
  90.             With .Cells(n, "R")
  91.                 .Value = rightQty
  92.                 .Font.Bold = True
  93.                 .Interior.ColorIndex = 8
  94.             End With
  95.             n = n + 1
  96.         Next
  97.     End With

  98. End Sub
复制代码

样式.zip

24.89 KB, 下载次数: 7

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-8 15:10 | 显示全部楼层
半百 发表于 2024-5-8 13:31
结果覆盖原表,请先备份原表。

QQ五笔截图未命名.png

您好,老师,,显示下标越界

TA的精华主题

TA的得分主题

发表于 2024-5-8 15:23 | 显示全部楼层
38180203 发表于 2024-5-8 15:10
您好,老师,,显示下标越界

brr的下界放大一些,
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-20 05:10 , Processed in 0.033737 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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