【发布时间】:2016-03-14 02:32:48
【问题描述】:
我收到一个运行时错误,但这可能是我遇到的最小问题。这个逻辑在我脑海中是有道理的,但我可能没有使用正确的语法或函数。我的代码在下面带有 cmets 和“希望”:
Sub Random_Points()
Dim i As Integer
Dim j As Integer
Dim Max As Integer
Dim RandomNumber As Integer
Max = 100 '(Max is being multiplied by the Rnd function to provide a random number between 0-100)
For i = 2 To 100 Step 1
RandomNumber = Int(Rnd * Max)
ThisWorkbook.Sheets("VBA").Cells(i, 2).Value = RandomNumber
'(The for loop above with start assigned cells values starting with Cells(2,2) to Cells(100,2))
'(I DO NOT WANT DUPLICATE VALUES...therefore after the value is assigned above I want the code to compare the newly assigned cell to all the cells above it.)
For j = 1 To 98 Step 1
'(...and IF the cell values are the same...)
If ThisWorkbook.Sheets("VBA").Cells(i, 2).Value = ThisWorkbook.Sheets("VBA").Cells(i - j, 2).Value Then
'(...A new random number will be assigned...)
RandomNumber = Int(Rnd * Max)
ThisWorkbook.Sheets("VBA").Cells(i, 2).Value = RandomNumber
End If
'(...and then re-checked vs all the others)
Next j
'(Next cell is assigned...loop restarts)
Next i
End Sub
【问题讨论】:
-
您是否调试并看到错误出现在哪一行?
-
虽然唯一随机数是矛盾的,你看到Generate 5000 records in 2 columns of random number that being unique了吗?
-
现在,请告诉我们您在哪一行遇到错误。
-
“IF”函数给了我错误。我去看看链接,谢谢!
标签: vba excel runtime-error