PPT VBA多幻灯片选择宏错误
当在输入框中输入多个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个问题。
a)如果您输入
2、4、5
,则检查的检查(strresponse2)
将失败,因为该功能试图将整个字符串转换为一个一个单个数字。b)
数组(iResponse2)
不会将字符串转换为数组(3个数字)。它将单字符串2、4、5
将其转换为具有1(不是3)成员的字符串数组。在您的情况下,您可以使用
split
- 功能将输入字符串拆分为字符串。c)如果要按数字访问幻灯片,则输入需要为数字类型,而不是字符串(即使字符串包含数字)。您需要将字符串数组转换为数字数组(如果将字符串或字符串数组作为参数,VBA将寻找具有 name 的成员,而不是 index )。
查看以下代码,并检查它是否需要您需要的东西 - 它只有一半的测试(因为我没有PowerPoint VBA可用,只有Excel,但Priniple是相同的)
Several issues here.
a) If you enter
2, 4, 5
, the check forIsNumeric(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 string2, 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)