|
楼主 |
发表于 2010-5-28 22:14
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
现在的的代码,可以对原先有内容的范围随机重排,对空白范围则自动随机填充数字
Sub RectangleDataRandom()
Dim a
Dim s As New Collection, t As New Collection
Randomize
a = Selection
For i = 0 To UBound(a, 1) - 1
For j = 0 To UBound(a, 2) - 1
s.Add a(i + 1, j + 1)
If b = "" Then b = b & a(i + 1, j + 1)
t.Add 1 + j Mod UBound(a, 2) + (i Mod UBound(a, 1)) * UBound(a, 2)
Next
Next
If b = "" Then
For i = 1 To UBound(a, 1)
For j = 1 To UBound(a, 2)
k = Int(Rnd() * t.Count + 1)
a(i, j) = t(k)
t.Remove (k)
'[a1].Resize(UBound(a, 1), UBound(a, 2)) = a
Next
Next
Else
For i = 1 To UBound(a, 1)
For j = 1 To UBound(a, 2)
k = Int(Rnd() * s.Count + 1)
a(i, j) = s(k)
s.Remove (k)
'[a1].Resize(UBound(a, 1), UBound(a, 2)) = a
Next
Next
End If
[a1].Resize(UBound(a, 1), UBound(a, 2)) = a
End Sub |
|