如何在此VBA脚本中停止重复的数字

时间:2015-11-15 23:33:06

标签: vba powerpoint powerpoint-vba

我找到了这个VBA脚本(在powerpoint中运行),我只是想知道如何阻止数字被重复。我做了一些谷歌搜索,我认为解决方案是创建一个数组,并让选定的数字进入数组。只要它跳过数组中的所有数字,脚本就会生成一个新数字。

我只是不确定如何实现这个,因为我不知道VBA。

这是脚本:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim chosenNum As Integer
Dim I As Integer
Dim k As Integer
Sub randomNumber()
    lowRand = 1
    maxRand = 10
    Randomize
    For k = 1 To 10
        chosenNum = Int((maxRand - lowRand) * Rnd) + lowRand
        With ActivePresentation.SlideShowWindow.View.Slide.Shapes(2).TextFrame.TextRange
            .Text = chosenNum
        End With
        For I = 1 To 1
            Sleep (50)
            DoEvents
        Next
    Next
End Sub

有什么想法?感谢。

3 个答案:

答案 0 :(得分:0)

这会将10个唯一的单个数字(0到9)收集到一个字符串中,然后将它们分成一个数组。当每个都返回到幻灯片时,添加1,因此resut是1到10。

Sub randomNumber()
    Dim lowRand As Long, maxRand As Long, strNum As String, chosenNum As String
    Dim k As Long, vNUMs As Variant

    lowRand = 0: maxRand = 10: strNum = vbNullString
    Randomize

    For k = 1 To 10
        chosenNum = Format(Int((maxRand - lowRand) * Rnd) + lowRand, "0")
        Do While CBool(InStr(strNum, chosenNum))
            chosenNum = Format(Int((maxRand - lowRand) * Rnd) + lowRand, "0")
        Loop
        strNum = strNum & chosenNum
    Next k

    vNUMs = Split(StrConv(strNum, vbUnicode), Chr(0))

    For k = LBound(vNUMs) To UBound(vNUMs)
        With ActivePresentation.SlideShowWindow.View.Slide.Shapes(2).TextFrame.TextRange
            .Text = vNUMs(k) + 1
        End With
        For I = 1 To 1
            Sleep (50)
            DoEvents
        Next
    Next k
End Sub

答案 1 :(得分:0)

我刚写了这个来帮助你。该函数用于返回指定范围内的随机整数,直到返回-1时返回范围内的所有数字。包含一个测试子,用于说明如何使用该函数从5到10获取所有数字。

'----------------------------------------------------------------------------------
' Copyright (c) 2015 YOUpresent Ltd.
' Source code is provide under Creative Commons Attribution License
' This means you must give credit for our original creation in the following form:
' "Includes code created by YOUpresent Ltd. (YOUpresent.co.uk)"
' Commons Deed @ http://creativecommons.org/licenses/by/3.0/
' License Legal @ http://creativecommons.org/licenses/by/3.0/legalcode
'----------------------------------------------------------------------------------

Option Explicit
Option Base 0 ' Explicitly set the lower bound of arrays to 0
Private iUsed As Integer ' count of all used numebrs

Public arrTracking() As String

'----------------------------------------------------------------------------------
' Purpose:  Returns a random number in a specified range without repeats
' Inputs:   iLow - integer representing the low end of the range
'           iHigh - integer representing the high end of the range
'           bReset - boolean flag to optionally reset the array
' Outputs:  returns an integer number or -1 if all numbers have been used
' Example first call: myNum = GetRandomNumber(10, 5, true)
' Example subsequent call: myNum = GetRandomNumber(10, 5)
'----------------------------------------------------------------------------------
Function GetRandomNumber(iLow As Integer, iHigh As Integer, Optional bReset As Boolean) As Integer
  Dim iNum As Integer     ' random number to be generated
  Dim InArray As Boolean  ' flag to test if number already used

  Randomize

  ' Reset the tracking array as required
  If bReset Then ReDim arrTracking(iHigh - iLow)

  ' If we've used all of the numbers, return -1 and quit
  If iUsed = iHigh - iLow + 1 Then
    GetRandomNumber = -1
    Exit Function
  End If

  ' Repeat the random function until we find an unused number and then
  ' update the tracking array, uncrease the counter and return the number
  Do While Not InArray
    iNum = Fix(((iHigh - iLow + 1) * Rnd + iLow))
    If arrTracking(iNum - iLow) = "" Then
      arrTracking(iNum - iLow) = "used"
      iUsed = iUsed + 1
      InArray = True
      GetRandomNumber = iNum
    Else
      'Debug.Print iNum & " used"
    End If
  Loop
End Function

'----------------------------------------------------------------------------------
' Purpose:  Test sub to get all random numbers in the range 5 to 10
' Inputs:   None
' Outputs:  Debug output of 6 numbers in the range 5 to 10 in then immediate window
'----------------------------------------------------------------------------------
Sub GetAllRand()
  Dim iRndNum As Integer
  ' Get the initial number, restting the tracking array in the process
  iRndNum = GetRandomNumber(5, 10, True)
  Debug.Print iRndNum
  Do While Not iRndNum = -1
    iRndNum = GetRandomNumber(5, 10)
    Debug.Print iRndNum
  Loop
End Sub

答案 2 :(得分:0)

这是一个UDF,您可以使用它来填充具有唯一随机数的数组:

Function GetRandomDigits(amount As Integer, maxNumber As Integer) As Variant
    With CreateObject("System.Collections.ArrayList")
        Do
            j = WorksheetFunction.RandBetween(1, maxNumber)
            If Not .Contains(j) Then .Add j
        Loop Until .Count = amount

        GetRandomDigits = .ToArray()
    End With
End Function

以下是如何使用它的示例:

Sub MM()

Dim nums As Variant

nums = GetRandomDigits(10, 100)

For Each num In nums
    Debug.Print num
Next

End Sub