使用 VBA 在幻灯片中显示随机数 [英] Display random number in slide using VBA

查看:67
本文介绍了使用 VBA 在幻灯片中显示随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成一个 1 到 30 之间的随机数并将其显示在每张幻灯片上.我在网上找到了以下代码:

I need to generate a random number between 1 and 30 and display it on every slide. I found the following code online:

Sub UpdateRandomNumber(oSh As Shape)
    Dim X As Long
    'Make the shape’s text a random number
    'X or less
    'Change 12 below to any number you’d like:
    X = 30
 
    oSh.TextFrame.TextRange.Text = CStr(Random(X))
End Sub
 
Function Random(High As Long) As Long
    'Generates a random number less than or equal to
    'the value passed in High
    Randomize
    Random = Int((High * Rnd) + 1)
End Function

Sub RandomNumber()

End Sub

我需要代码以不同的方式做一件事:
提示操作的对象位于所有幻灯片上的同一位置.生成和显示随机数时,所有幻灯片都应相应更改.
当我离开幻灯片时,应该显示之前生成的数字,而不是之前在这张幻灯片上生成的数字.

I need the code to do one thing differently:
The object prompting the action is in the same spot on all slides. When generating and displaying a random number, all slides should be changed accordingly.
When I leave the slide, the previously generated number should be shown instead of the one that was previously generated on this slide.

推荐答案

这会创建一个 1 到 30 之间的随机数.将形状名称更改为文件中使用的实际形状:

This creates a random number between 1 and 30. Change the shape name to the actual shape used in your file:

Sub ShapeNumber()
    Dim X As Long
    Dim ShapeNumber As String
    Dim oSlide As Slide
    Dim oShape As Shape

    X = 30
    Randomize
    ShapeNumber = Int((X * Rnd) + 1)
    For Each oSlide In ActivePresentation.Slides
        For Each oShape In oSlide.Shapes
            If oShape.Name = "Rectangle 3" Then
                oShape.TextFrame.TextRange.Text = ShapeNumber
            End If
        Next oShape
    Next oSlide
End Sub

这篇关于使用 VBA 在幻灯片中显示随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆