【发布时间】:2017-10-11 09:31:39
【问题描述】:
我对 VBA 还很陌生。
我正在尝试使用 VBA 随机化一个列表。该列表有两个标题“名称”和“拨号”。我想尝试使用宏随机化列表,然后使用按钮应用它。我尝试过使用下面的代码,但它会随机化名称和数字,但不会将它们放在一起。意思是如果我的名字是乔恩并且我有 3 个表盘,它会将我的表盘移动到其他地方。任何帮助将不胜感激。
谢谢,
Sub Random()
Dim tempString As String
Dim tempInteger As Integer
Dim i As Integer
Dim j As Integer
For i = 1 To 5
Cells(i, 2).Value = WorksheetFunction.RandBetween(0, 1000)
Next i
For i = 1 To 5
For j = i + 1 To 5
If Cells(j, 2).Value < Cells(i, 2).Value Then
tempString = Cells(i, 2).Value
Cells(i, 2).Value = Cells(j, 2).Value
Cells(j, 2).Value = tempString
tempInteger = Cells(i, 2).Value
Cells(i, 2).Value = Cells(j, 2).Value
Cells(j, 2).Value = tempInteger
End If
Next j
Next i
End Sub
【问题讨论】:
-
在代码的随机部分之前调用
Randomize -
另外,如果你有'a'列表,你怎么有两个标题?查看数据会有所帮助。
-
向列表中添加另一列并用随机值填充该列。然后使用该列对列表进行排序。
-
您使用了this Tutorial,仅用于一列。跟 jsotola 说的差不多。