20170528xlVBA凑数一例
Posted Excel VBA 小天地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20170528xlVBA凑数一例相关的知识,希望对你有一定的参考价值。
Public Sub MakeUp() Dim Sht As Worksheet Set Sht = ThisWorkbook.Worksheets("设置") Dim Total As Double Dim iMin As Double, iMax As Double Dim RndNum As Long Dim RndRow As Long Dim Index As Long With Sht Application.Intersect(.Range("C:C"), .UsedRange.Offset(1)).ClearContents Total = .Range("B2").Value iMin = .Range("B3").Value iMax = .Range("B4").Value Index = 1 ‘初次分配 Do While Total > iMax Index = Index + 1 RndNum = iMin + Rnd() * (iMax - iMin) .Cells(Index, 3).Value = RndNum Total = Total - RndNum Loop ‘产生剩余 If Total >= iMin Then .Range("B5").Value = Index Index = Index + 1 .Cells(Index, 3).Value = Total Else ‘剩余不足2900的 再次随机分配 Do While Total > 0 RndRow = Rnd() * (Index - 2) + 2 Delta = iMax - .Cells(RndRow, 3).Value If Total > Delta Then RndNum = Rnd() * (Delta) ‘保证不会超过3500 .Cells(RndRow, 3).Value = .Cells(RndRow, 3).Value + RndNum Total = Total - RndNum Else .Cells(RndRow, 3).Value = .Cells(RndRow, 3).Value + Total Total = 0 End If Loop .Range("B5").Value = Index End If ‘If Now > #10/1/2017# Then Application.Intersect(.Range("C:C"), .UsedRange.Offset(1)).ClearContents End With Set Sht = Nothing End Sub
以上是关于20170528xlVBA凑数一例的主要内容,如果未能解决你的问题,请参考以下文章
对“xxx”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。 错误解决一例。(代码片段