完全对此感到困惑: 我有一张表,我希望从给定列中随机选择一个单元格,然后用新值填充它。我也希望能够重复随机数序列。问题是,我不知道程序如何计算NewQuantity或程序为什么选择它所执行的单元格。它似乎使用的随机数不是我用Watch看到的那些。我已多次介绍它,我的计算总是在确定NewQuantity或选择单元格时完成。下面的代码 - 我已经通过使用$$ n $$(n = 1或2作为第1行和第2行)开始行来注意到所关注的代码。
以下是对正在发生的事情的一些观察: 1.当执行代码时,rnd()在每一行上创建一个新的随机数 - 无论它们是否是rnd()语句。 2.当我逐步介绍相关代码行时(例如$$ 1 $$和$$ 2 $$),这里是一系列变量值。
在进入第一行之前:rnd()=。980176,NewQuantity = 428,Int((CountCells - 1)* Rnd()+ 2)= 51。
第一行:rnd()=。764067,NewQuantity = 428,Int((CountCells - 1)* Rnd()+ 2)= 16.
在第一行之后:rnd()=。442,NewQuantity = 335,Int((CountCells - 1)* Rnd()+ 2)= 23.
在进入第二行之前:rnd()=。980176,NewQuantity = 335,Int((CountCells - 1)* Rnd()+ 2)= 49
在第二行:rnd()=。9688,NewQuantity = 335,Int((CountCells - 1)* Rnd()+ 2)= 17。
在第一行之后:rnd()=。203,NewQuantity = 367,Int((CountCells - 1)* Rnd()+ 2)= 39。
我很乐意向任何人发送包含数据和宏的工作表。
感谢您的帮助。
Function RandomizeData(stdID As Long)
'
Dim CountCells As Integer ' number of cells in a column
Const RandCount As Integer = 5 ' number of cells in each table to randomly change
Dim NewQuantity As Integer ' new randomly determined quantity
Dim NewPrice As Single ' new randomly determined price
Const LowQuantity = 5 ' Lowest quantity to be randomly created
Const HighQuantity = 500 ' highest quantity to be randomly created
Const LowPrice As Single = 0.5 ' lowest percent change to current price
Const HighPrice As Single = 1.2 ' highest percent change to current price
Dim ExistingPrice As Variant ' Set parameters for changing quantity and price
Dim RedID As Long ' RedID number
'
' Set parameters for changing quantity and price
'
' Make random changes to the quantity In the order quantities
RedID = stdID
Rnd (-4)
Randomize (RedID)
Worksheets("Order_Detail").Select
CountCells = WorksheetFunction.Count(Range("C2:C59"))
'Determine random quantity change to a random cell in Order Details Quantity
For j = 1 To RandCount
$$1$$ NewQuantity = Int((HighQuantity - LowQuantity + 1) * Rnd() + LowQuantity)
$$2$$ Cells(Int((CountCells - 1) * Rnd() + 2), "C").Value = NewQuantity
Next j
'
End Function