Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2872|回复: 2

[已解决]数的分解?

[复制链接]
发表于 2014-4-6 15:19 | 显示全部楼层 |阅读模式
如何能用一段代码实现将一个总数分解成若干个随机生成的数?
最佳答案
2014-4-6 17:13
  1. Sub Macro1()
  2. Dim arr(1 To 50, 1 To 1), i%, n%
  3. Range("f3:j100").ClearContents
  4. For i = 1 To 5
  5. line1:
  6.     s = 0: n = 0: x = 100
  7.     Do While s < x
  8.         y = Application.Round(Rnd * 6 + 2, 2)
  9.         s = s + y
  10.         n = n + 1
  11.         arr(n, 1) = y
  12.         If s >= 92 Then Exit Do
  13.     Loop
  14.     If x - s < 2 Then GoTo line1
  15.     arr(n + 1, 1) = x - s
  16.     Cells(3, i + 5).Resize(n + 1) = arr
  17.     Erase arr
  18. Next
  19. End Sub
复制代码

分解.rar

8.54 KB, 下载次数: 8

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-4-6 17:13 | 显示全部楼层    本楼为最佳答案   
  1. Sub Macro1()
  2. Dim arr(1 To 50, 1 To 1), i%, n%
  3. Range("f3:j100").ClearContents
  4. For i = 1 To 5
  5. line1:
  6.     s = 0: n = 0: x = 100
  7.     Do While s < x
  8.         y = Application.Round(Rnd * 6 + 2, 2)
  9.         s = s + y
  10.         n = n + 1
  11.         arr(n, 1) = y
  12.         If s >= 92 Then Exit Do
  13.     Loop
  14.     If x - s < 2 Then GoTo line1
  15.     arr(n + 1, 1) = x - s
  16.     Cells(3, i + 5).Resize(n + 1) = arr
  17.     Erase arr
  18. Next
  19. End Sub
复制代码
回复

使用道具 举报

发表于 2014-4-6 17:14 | 显示全部楼层
………………

分解.zip

9.64 KB, 下载次数: 8

回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 22:03 , Processed in 0.385856 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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