Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3922|回复: 2

[已解决]Set s = CreateObject("MSScriptControl.ScriptControl")这一行出错了

[复制链接]
发表于 2015-6-3 15:15 | 显示全部楼层 |阅读模式
Sub CommandButton1_Click()
tmp = [a65536].End(xlUp).Row
z = InputBox("请输入排列数(<=" & tmp - 1 & "):", "排列组合", tmp - 1)
If z = "" Then Exit Sub
If (Not IsNumeric(z)) Then Exit Sub
z = Int(z)
If z > tmp - 1 Or z < 1 Then
MsgBox "输入参数错误!"
Exit Sub
End If
Set D = CreateObject("scripting.dictionary")
Range("A2:A" & tmp).Select
arr = Selection.Value
r = UBound(arr)
vb = "sub my1(arr):"
st = ""
st1 = ""
For i = 1 To z
st = st & "arr(i" & i & ",1) & "
vb = vb & "for i" & i & " = 1 to " & r & ":"
If i > 1 Then
st1 = st1 & "end if:next:"
st2 = "if"
For j = 1 To i - 1
st2 = st2 & " i" & i & "<> i" & j & " and "
Next
vb = vb & Left(st2, Len(st2) - 4) & " then:"
End If
Next
vb = vb & "d(" & Left(st, Len(st) - 3) & ")=0:" & st1 & "next:end sub:"

Set s = CreateObject("MSScriptControl.ScriptControl")
     s.Language = "VBScript"
     s.AddCode vb
     s.AddObject "d", D
     s.Run "my1", arr
Range("B2:B65536").Clear
[b2].Resize(D.Count, 1) = Application.Transpose(D.Keys)

End Sub


Set s = CreateObject("MSScriptControl.ScriptControl")这一行出错了,我的表格是2013的,高手帮我看看,谢谢!!!64位系统
最佳答案
2015-6-3 16:37
本帖最后由 roych 于 2015-6-3 16:39 编辑

单看这一句是没问题的。问题在于你的VBSCript语句,下面这段供参考(请拿strProgram来对比你的VBScript语句):
  1. Sub test()
  2. Dim strProgram As String
  3. Set sc = CreateObject("MSScriptControl.ScriptControl")
  4.        '编写代码
  5.        strProgram = "Sub My1" & vbCrLf & _
  6.     "MsgBox ""运行时设置代码"" ,vbInformation,""我的代码""" & vbCrLf & _
  7.     "End Sub"
  8.        '设置代码语言,并将代码添加到 ScriptControl
  9.        Set sc = CreateObject("ScriptControl")
  10.        sc.Language = "VBScript"
  11.        sc.AddCode strProgram
  12.        '运行代码
  13.        sc.Run "My1"
  14. End Sub
复制代码
只是好端端的干吗用VBScript呢?如果是在Excel或者Access上处理的话,直接调用sub或者function就可以了啊
发表于 2015-6-3 16:08 | 显示全部楼层
回复

使用道具 举报

发表于 2015-6-3 16:37 | 显示全部楼层    本楼为最佳答案   
本帖最后由 roych 于 2015-6-3 16:39 编辑

单看这一句是没问题的。问题在于你的VBSCript语句,下面这段供参考(请拿strProgram来对比你的VBScript语句):
  1. Sub test()
  2. Dim strProgram As String
  3. Set sc = CreateObject("MSScriptControl.ScriptControl")
  4.        '编写代码
  5.        strProgram = "Sub My1" & vbCrLf & _
  6.     "MsgBox ""运行时设置代码"" ,vbInformation,""我的代码""" & vbCrLf & _
  7.     "End Sub"
  8.        '设置代码语言,并将代码添加到 ScriptControl
  9.        Set sc = CreateObject("ScriptControl")
  10.        sc.Language = "VBScript"
  11.        sc.AddCode strProgram
  12.        '运行代码
  13.        sc.Run "My1"
  14. End Sub
复制代码
只是好端端的干吗用VBScript呢?如果是在Excel或者Access上处理的话,直接调用sub或者function就可以了啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-10 09:29 , Processed in 0.243476 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表