首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在VBA for Powerpoint上使随机数不重复?

如何在VBA for Powerpoint上使随机数不重复?
EN

Stack Overflow用户
提问于 2021-03-29 00:16:39
回答 1查看 33关注 0票数 0

我在PowerPoint VBA上做了一个单词抽奖,但我想知道如何使随机不重复,并可能在所有单词都被使用时停止?从那以后谢谢你,我想把所有的功劳都给那个宏的创建者。

我使用的代码

代码语言:javascript
复制
    Dim x As New Collection

        Sub box()
        Dim items() As String
        Dim y As Long

        items = Split("Blinding\ Nightmare\ Ice cream\", "\")
        For y = 0 To UBound(items)
        x.Add (items(y))
        Next y
        End Sub

        Sub wordspick()
        Dim words As Long
        Randomize
        y = Int(Rnd * x.Count) + 1
        Shapes("x").TextFrame.TextRange.Text = x(y)
        x.Remove (y)
        End Sub
EN

回答 1

Stack Overflow用户

发布于 2021-03-29 14:24:36

代码语言:javascript
复制
Dim x As Collection

Sub box()
    Dim items() As String
    Dim y As Long
    
    Set x = New Collection 'clear x, in case not empty
    items = Split("Blinding\Nightmare\Ice cream", "\")
    For y = 0 To UBound(items)
        x.Add items(y)
    Next y
    Randomize
End Sub

Sub wordspick()
    Dim words As Long, y As Long
    
    If Not x Is Nothing Then 'check collection is initialized
        If x.Count = 0 Then
            Debug.Print "No more words!" 'collection is empty
        Else
            y = Int(Rnd * x.Count) + 1
            'Shapes("x").TextFrame.TextRange.Text = x(y)
            Debug.Print x(y)
            x.Remove y
        End If
    End If
    
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66843526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档