【发布时间】:2019-10-20 02:43:31
【问题描述】:
在高层次上,我试图将一个单元格设置为一个范围内的随机单元格。我遇到的问题是,在这个范围内我想从中提取一个随机值,我所取的值是一个“if”表达式的结果,它将单元格设置为一个值或“”。因此,当我选择随机值时,我只想选择具有实际值的单元格,而不是“”。
有谁知道如何获得这种预期的行为?
下面的代码显示了我目前尝试过的内容,每个大块都有注释以帮助理解。我需要帮助的块替换每一列中的值,直到下一个单元格为空白,然后移动到下一列。
upperBound = 1798
lowerBound = 2
Randomize
'This loop section populates the data area with a static value in cell 9,3 then 9,4 etc..
For j = 3 To 15
val = Cells(9, j).Value
For i = 1 To val
Cells(12 + i, j).Value = Cells(9, j)
Next i
Next j
'This loop section uses the cells already populated down each column and replaces that value with the random value from the other range
Dim x As Integer
' Set numrows = number of rows of data.
For j = 3 To 15
NumRows = Range(Cells(13, j), Cells(13, j).End(xlDown)).Rows.Count
' Select cell 13,j.
Cells(13, j).Select
' Establish "For" loop to loop "numrows" number of times.
For x = 1 To NumRows
ActiveCell.Value = Worksheets("2017 Role IDs").Cells(Int((upperBound - lowerBound + 1) * Rnd + lowerBound), 2).Value
' Selects cell down 1 row from active cell.
ActiveCell.Offset(1, 0).Select
Next
Next j
这是第二个块运行之前的数据。我想将与第二行中的数字匹配的值替换为范围内的随机数:
这就是我想要的样子:
但目前它看起来像这样,因为随机选择器采用空白值:
【问题讨论】:
-
查看样本数据和预期输出会很有用
-
@cybernetic.nomad 已更新,感谢您的反馈!