点击插页>模块,然后将以下代码粘贴到模块窗口。 PublicFunctionRandomNumbers(Num1AsLong,Num2AsLong,OptionalDecimalsAsInteger)'Update20131113Application.Volatile RandomizeIfIsMissing(Decimals)OrDecimals=0ThenRandomNumbers=Int((Num2+1-Num1)*Rnd+Num1)ElseRandomNumbers=Round((Num2-Num1)*Rnd+Num1,Decimals)...
Sub RandomNumberSheet() Dim M As Integer For M = 1 To 5 ActiveSheet.Cells(M, 1) = Round((Rnd(10) * 7) + 3, 0) Next M End Sub 还有一个名为Randomize的 VBA 函数。可以在Rnd函数之前使用它来将种子值重置为计时器事件或任何给定的参数 Sub RandomNumberV2() Randomize (10) MsgBox Round(...
Int是integer的缩写,表示整数的意思,因此int会对数值进行向下的取整处理,即取值的结果不大于原来的数值语法:=int(数值), =int() 说明: =int(3.45),返回3 =int(-3.45),返回-4 注意点:向下取整,尤其是负数是的结果需要理解一下。3.rounddown函数 Round+down,向下指定位数舍入。语法:=rounddown(数值,舍入...
可以用字符串先连接起已生成的数字,再判断新数字是否已存在于旧字符串中,如果存在的话就重复再跑一次即可: SubCreateRandomUniqueNumbers()DimrngAsRange,randNumberAsInteger,randNumberStringAsStringForEachrngInActiveSheet.Range("A1:A5")Repeat:randNumber=Application.WorksheetFunction.RandBetween(1,5)IfVBA.InStr(r...
Sub RandomNumberSheet() Dim M As Integer For M = 1 To 5 ActiveSheet.Cells(M, 1) = Round((Rnd(10) * 7) + 3, 0) Next M End Sub 还有一个名为Randomize的 VBA 函数。可以在Rnd函数之前使用它来将种子值重置为计时器事件或任何给定的参数 ...
Sub Range_RandomNumber() 'Updateby Extendoffice Dim xStrRange As String Dim xRg, xCell, xRg1 As Range Dim xArs As Areas Dim xNum_Lowerbound As Integer Dim xNum_Upperbound As Integer Dim xI, xJ, xS, xR As Integer xStrRange = "A1:B20" xNum_Lowerbound = 100 xNum_Upperbound =...
Sub RandomNumberSheet() Dim M As Integer For M = 1 To 5 ActiveSheet.Cells(M, 1) = Round((Rnd(10) * 7) + 3, 0) Next M End Sub 还有一个名为Randomize的 VBA 函数。可以在Rnd函数之前使用它来将种子值重置为计时器事件或任何给定的参数 Sub RandomNumberV2() Randomize (10) MsgBox Round(...
Sub RandomNumberSheet() Dim M As Integer For M = 1 To 5 ActiveSheet.Cells(M, 1) = Round((Rnd(10) * 7) + 3, 0) Next M End Sub 还有一个名为Randomize的 VBA 函数。可以在Rnd函数之前使用它来将种子值重置为计时器事件或任何给定的参数 ...
Sub Range_RandomNumber() 'Updateby Extendoffice Dim xStrRange As String Dim xRg, xCell, xRg1 As Range Dim xArs As Areas Dim xNum_Lowerbound As Integer Dim xNum_Upperbound As Integer Dim xI, xJ, xS, xR As Integer xStrRange = "A1:B20" xNum_Lowerbound = 100 xNum_Upperbound =...
Generate random integer numbers between two numbers To generate some random integers, you should combine the RAND and INT functions together as shown in the table below:Formula Description =INT(RAND()*N) Generate random integers between 0 and N. =INT(RAND()*(B-A)+A) Generate random ...