ddbb2008 发表于 2013-11-5 15:42:33

未定义类型 错误,无法运行vba

http://bbs.mjtd.com/thread-96754-1-1.html
提取块属性的代码:

[*]Sub aa()

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifDim bobj As AcadBlockReference

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifDim a

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifDim arr()

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifDim ss As AcadSelectionSet

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifDim filtertype(0 To 0) As Integer

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifDim filterdata(0 To 0) As Variant

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifDim xls As Excel.Application

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifSet xls = New Excel.Application

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifSet ss = ThisDrawing.SelectionSets.Add(CStr(Rnd))

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.giffiltertype(0) = 0

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.giffilterdata(0) = "insert"

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifss.SelectOnScreen filtertype, filterdata

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifFor Each bobj In ss

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifi = i + 1

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifReDim Preserve arr(1 To i)

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifa = bobj.GetAttributes

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifarr(i) = a(0).TextString

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifNext

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifWith xls.Workbooks.Add

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif   .Sheets(1)..Resize(i, 1) = xls.Transpose(arr)

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif   .SaveAs "d:/123.xls"

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif   .Close

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifEnd With

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifxls.Quit

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gifEnd Sub
为什么我使用代码运行会出现这样的错误呢




wylong 发表于 2013-11-6 16:10:44

打开VBA IDE工具-引用,在引用里面勾选“Microsoft Excel14.0ObjectLibrary”。(注:如果你安装的EXCEL为2010;其他版本类似)

ddbb2008 发表于 2013-11-6 18:30:47

wylong 发表于 2013-11-6 16:10 static/image/common/back.gif
打开VBA IDE工具-引用,在引用里面勾选“Microsoft Excel14.0ObjectLibrary”。(注:如果你安装的EXC ...

谢谢你的回复, 我装的是07版excel 引用以后,代码可以运行了,但是出现这个,是怎么回事?

ddbb2008 发表于 2013-11-6 18:34:25

wylong 发表于 2013-11-6 16:10 static/image/common/back.gif
打开VBA IDE工具-引用,在引用里面勾选“Microsoft Excel14.0ObjectLibrary”。(注:如果你安装的EXC ...

我把代码中得xls改成xlsx 以后就变成这样,实在不知道问题在哪儿,,,,

wylong 发表于 2013-11-7 12:09:01

错误提示的是命名选择集已存在。
问题在此句:Set ss = ThisDrawing.SelectionSets.Add(CStr(Rnd))

建议改为这样:Set ss = CreateSelectionSet

CreateSelectionSet函数定义如下:
'返回一个空选择集
Public Function CreateSelectionSet(Optional SelectionSetName As String = "mjtd") As AcadSelectionSet
    On Error Resume Next
    ThisDrawing.SelectionSets(SelectionSetName).Delete
    Set CreateSelectionSet = ThisDrawing.SelectionSets.Add(SelectionSetName)
    On Error GoTo 0
End Function
页: [1]
查看完整版本: 未定义类型 错误,无法运行vba