![](/img/trans.png)
[英]How to randomly distribute a known group of numbers into a column using Excel / VBA
[英]How to randomly distribute some available data in excel (a million entries) to a group of 10 columns/rows?
我有一列员工的“ ID”(大约100万个条目),我想将它们随机分配到新的Excel工作表中的新10行/列中。
例如:
Card ID
123
132
143
...
我想将此数据随机分配到新的Excel工作表中的10个新列/行(无论可行)。
此过程是完成此任务的几种方法之一:
Sub randomColumns()
Const inputSheet = "Sheet1" 'name of the sheet with the million values
Const inputColumn = 1 'column # where the million values are
Const inputStartRow = 1 'rows # of the first value
Const outputColumns = 10 'number of columns randomize into
Dim wsIn As Worksheet, wsOut As Worksheet
Dim rw As Long, inValue As Variant, outCol As Long
Set wsIn = Sheets(inputSheet) 'setup worksheets
Set wsOut = ActiveWorkbook.Worksheets.Add 'create new worksheet
rw = inputStartRow
Randomize 'seed random number generator
With wsOut
Do
inValue = wsIn.Cells(rw, inputColumn) 'get the value to move
outCol = Int(Rnd() * outputColumns) + 1 'pick random column
.Cells(Application.WorksheetFunction. _
CountA(.Columns(outCol)) + 1, outCol) = inValue 'move value
wsIn.Cells(rw, inputColumn) = "" 'remove value from old location
rw = rw + 1 'next input row
Loop While wsIn.Cells(rw, inputColumn) <> "" 'loop until blank cell
End With
MsgBox "Randomized " & rw - 1 & " items to worksheet '" & wsOut.Name & "'."
End Sub
将常量inputSheet
的值更改为带有值的工作表的名称。 如有必要,请更改其他常量,否则它将开始在inputSheet
A1
中查找值。
将创建一个新的工作表,然后将每个数字放在随机选择的列中值的底部,然后从inputSheet
删除。
这将继续,直到在inputSheet
上遇到空白单元格inputSheet
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.