PPT VBA多幻灯片选择宏错误

发布于 2025-02-06 07:49:36 字数 519 浏览 2 评论 0原文

当在输入框中输入多个PowerPoint幻灯片号(例如:3、5、6)时,我想创建一个宏来选择输入号码的幻灯片,但会发生错误。

Sub test()

Dim strresponse2 As String
Dim iresponse2 As String
strresponse2 = InputBox("page number" & vbCr & "ex) 2,4,11,5")

If IsNumeric(strresponse2) Then
iresponse2 = strresponse2
End If

ActiveWindow.Selection.Unselect
ActivePresentation.slides.Range(Array(iresponse2)).Select
'error here
'How to fix it so it doesn't get an error

'ActivePresentation.Slides.Range(Array(2, 4, 11,5)).Select
'no error

End Sub

When multiple PowerPoint slide numbers are entered in the input box (ex: 3, 5, 6), I want to create a macro that selects the slides of the entered number, but an error occurs.

Sub test()

Dim strresponse2 As String
Dim iresponse2 As String
strresponse2 = InputBox("page number" & vbCr & "ex) 2,4,11,5")

If IsNumeric(strresponse2) Then
iresponse2 = strresponse2
End If

ActiveWindow.Selection.Unselect
ActivePresentation.slides.Range(Array(iresponse2)).Select
'error here
'How to fix it so it doesn't get an error

'ActivePresentation.Slides.Range(Array(2, 4, 11,5)).Select
'no error

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2025-02-13 07:49:37

这里有几个问题。
a)如果您输入2、4、5,则检查的检查(strresponse2)将失败,因为该功能试图将整个字符串转换为一个一个单个数字。

b)数组(iResponse2)不会将字符串转换为数组(3个数字)。它将单字符串2、4、5将其转换为具有1(不是3)成员的字符串数组。
在您的情况下,您可以使用split - 功能将输入字符串拆分为字符串。

c)如果要按数字访问幻灯片,则输入需要为数字类型,而不是字符串(即使字符串包含数字)。您需要将字符串数组转换为数字数组(如果将字符串或字符串数​​组作为参数,VBA将寻找具有 name 的成员,而不是 index )。

查看以下代码,并检查它是否需要您需要的东西 - 它只有一半的测试(因为我没有PowerPoint VBA可用,只有Excel,但Priniple是相同的)

Dim answer As String
answer = InputBox("page number" & vbCr & "ex) 2,4,11,5")
Dim pagesS() As String

pagesS = Split(answer, ",")                 ' Split the answer into an array of strings.
ReDim pagesN(0 To UBound(pagesS)) As Long   ' Create an empty numeric array
Dim countS As Long, countN As Long 
For countS = 0 To UBound(pagesS)            ' Loop over all strings
    If IsNumeric(pagesS(countS)) Then       ' String is Numeric
        Dim pageNo As Long
        pageNo = Val(pagesS(countS))        ' Convert string to number
        If pageNo > 0 And pageNo <= ActivePresentation.slides.Count Then
            pagesN(countN) = pageNo         ' When number is within valid range, copy it 
            countN = countN + 1             ' Count the number of valid page numbers
        End If
    End If
Next countS

If countN > 0 Then                          ' At least one number found
    ReDim Preserve pagesN(0 To countN - 1)  ' Get rid of unused elements
    ActivePresentation.Slides.Range(pagesN).Select    
End If

Several issues here.
a) If you enter 2, 4, 5, the check for IsNumeric(strresponse2) will fail because the function tries to convert the whole string into one single number.

b) Array(iresponse2) will not convert the string into an array (of 3 numbers). It will convert the single string 2, 4, 5 into an string array with 1 (not 3) member.
In your case, you can use the Split-function to split the input string into an array of strings.

c) If you want to access the slides by number, the input needs to be of a numeric type, not of string (even if the strings contain numbers). You will need to convert the string array into a numeric array (if you pass a string or an array of strings as parameter, VBA will look for members with the name, not the index).

Have a look to the following piece of code and check if it does what you need - it's only half tested (as I have no Powerpoint VBA available, only Excel, but the priniple is the same)

Dim answer As String
answer = InputBox("page number" & vbCr & "ex) 2,4,11,5")
Dim pagesS() As String

pagesS = Split(answer, ",")                 ' Split the answer into an array of strings.
ReDim pagesN(0 To UBound(pagesS)) As Long   ' Create an empty numeric array
Dim countS As Long, countN As Long 
For countS = 0 To UBound(pagesS)            ' Loop over all strings
    If IsNumeric(pagesS(countS)) Then       ' String is Numeric
        Dim pageNo As Long
        pageNo = Val(pagesS(countS))        ' Convert string to number
        If pageNo > 0 And pageNo <= ActivePresentation.slides.Count Then
            pagesN(countN) = pageNo         ' When number is within valid range, copy it 
            countN = countN + 1             ' Count the number of valid page numbers
        End If
    End If
Next countS

If countN > 0 Then                          ' At least one number found
    ReDim Preserve pagesN(0 To countN - 1)  ' Get rid of unused elements
    ActivePresentation.Slides.Range(pagesN).Select    
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文