ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 求单元格内数值凑数函数或VBA

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-24 11:03 | 显示全部楼层 |阅读模式
各位坛友

本人想写一个函数或VBA来进行凑数,论坛上的递归VBA我也看了,没有弄明白,想请精通的坛友帮忙!
非常非常感谢!
附件是用office365做的 求单元格内数值凑数函数或VBA.zip (8.84 KB, 下载次数: 16)

TA的精华主题

TA的得分主题

发表于 2024-5-24 16:32 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
光看你这一圈结果完全不知道你想要啥,比如,工程1里,为什么只有这四种组合,小于6000的可能性绝对不止4种,你要准确描述你的需求才有可能得到有效的帮助!

TA的精华主题

TA的得分主题

发表于 2024-5-24 16:44 | 显示全部楼层
  1. Sub MakeUpNumbers()
  2. Dim TargetNumber As Double
  3. Dim Numbers As Variant
  4. Dim OutputRow As Integer
  5. Dim RequiredCombinations As Integer

  6. ' 设置目标数值
  7. TargetNumber = 6000

  8. ' 初始化数字数组
  9. Numbers = Array(1354.5, 1486, 1643.7, 1821, 2053, 3255, 3679, 3832)

  10. ' 计算所需的组合数量
  11. RequiredCombinations = WorksheetFunction.RoundUp((1354.5 + 1486 + 1643.7 + 1821 + 2053 + 3255 + 3679 + 3832) / 6000, 0)

  12. ' 初始化输出行
  13. OutputRow = 1

  14. ' 调用函数生成数字组合
  15. FindNumbers TargetNumber, Numbers, OutputRow, RequiredCombinations
  16. End Sub

  17. Sub FindNumbers(Target As Double, Numbers As Variant, OutputRow As Integer, RequiredCombinations As Integer)
  18. Dim i As Integer, j As Integer, k As Integer
  19. Dim totalNumbers As Integer
  20. Dim totalCombinations As Integer
  21. Dim sum As Double
  22. Dim combination As String
  23. Dim count As Integer
  24. Dim usedNumbers As String
  25. Dim closestSum As Double
  26. Dim closestCombination As String
  27. Dim closestDiff As Double
  28. Dim foundCombinations As Integer

  29. closestSum = 0
  30. closestCombination = ""
  31. closestDiff = Target
  32. foundCombinations = 0

  33. totalNumbers = UBound(Numbers) - LBound(Numbers) + 1
  34. totalCombinations = 2 ^ totalNumbers

  35. For i = 1 To totalCombinations - 1
  36. sum = 0
  37. combination = ""
  38. count = 0
  39. usedNumbers = ""

  40. For j = 0 To totalNumbers - 1
  41.     If ((i And (2 ^ j)) <> 0) Then
  42.         If InStr(usedNumbers, CStr(Numbers(LBound(Numbers) + j))) = 0 Then
  43.             sum = sum + Numbers(LBound(Numbers) + j)
  44.             combination = combination & Numbers(LBound(Numbers) + j) & "+"
  45.             count = count + 1
  46.             usedNumbers = usedNumbers & CStr(Numbers(LBound(Numbers) + j)) & ","
  47.         End If
  48.     End If
  49. Next j

  50. If count >= 2 And sum <= Target Then
  51.     combination = Left(combination, Len(combination) - 1) ' 移除最后一个加号
  52.     If Abs(Target - sum) <= closestDiff Then
  53.         closestSum = sum
  54.         closestCombination = combination
  55.         closestDiff = Abs(Target - sum)
  56.         If Abs(Target - sum) < closestDiff Then
  57.             foundCombinations = 1
  58.         ElseIf Abs(Target - sum) = closestDiff Then
  59.             foundCombinations = foundCombinations + 1
  60.         End If
  61.         Worksheets("Sheet1").Cells(OutputRow, 3).Value = closestCombination
  62.         OutputRow = OutputRow + 1
  63.     End If
  64. End If

  65. If foundCombinations >= RequiredCombinations Then
  66.     Exit For
  67. End If
  68. Next i

  69. If foundCombinations = 0 Then
  70. MsgBox "未找到符合条件的数字组合。"
  71. Else
  72. MsgBox "找到 " & foundCombinations & " 个组合,组合已成功写入C列"
  73. End If
  74. End Sub
复制代码
用GPT生成的代码,没用递归,供参考吧
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-6-17 03:13 , Processed in 0.032798 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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