Lỗi subscript out of range khi dùng main cad2007

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Subscript out of range (Error 9)

  • Article
  • 09/13/2021

In this article

Elements of and members of can only be accessed within their defined ranges. This error has the following causes and solutions:

  • You referenced a nonexistent array element. The subscript may be larger or smaller than the range of possible subscripts, or the array may not have dimensions assigned at this point in the application. Check the of the array to verify its upper and lower bounds. Use the UBound and LBound functions to condition array accesses if you are working with arrays that are redimensioned. If the index is specified as a , check the spelling of the variable name.
  • You declared an array but didn't specify the number of elements. For example, the following code causes this error:

    Dim MyArray() As Integer MyArray(8) = 234 ' Causes Error 9.

    Visual Basic doesn't implicitly dimension unspecified array ranges as 0 - 10. Instead, you must use Dim or ReDim to specify explicitly the number of elements in an array.
  • You referenced a nonexistent collection member. Try using the For Each...Next construct instead of specifying index elements.
  • You used a shorthand form of subscript that implicitly specified an invalid element. For example, when you use the ! operator with a collection, the ! implicitly specifies a key. For example, object!keyname. value is equivalent to object. item (keyname). value. In this case, an error is generated if keyname represents an invalid key in the collection. To fix the error, use a valid key name or index for the collection.

For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).

Note

Interested in developing solutions that extend the Office experience across multiple platforms? Check out the new Office Add-ins model. Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML.

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

I'm having trouble with the following code. In line 10 I get a 'Subscript out of range' error on intPoints(2). I followed the IntersectWith code example in the help file but don't see where mine should operate any different than theirs.

They dimmed intPoints as Variant and so did I. They then extracted point info from the variable (intPoints(j)) and so did I (intPoints(2)).

Yet I get the error. And the variable LinLen always remains empty.

What am I doing wrong? bc

Sub Intersect()

'This code is supposed to find the intersection point between 'a line and a 3dFace, check the length of the line stretched 'to that point then find an area based on that length. Dim msg As String

Dim ssetObj1 As AcadSelectionSet Dim ssetObj2 As AcadSelectionSet

Set ssetObj1 = ThisDrawing.SelectionSets.Add("TopLines") Set ssetObj2 = ThisDrawing.SelectionSets.Add("TopFaces")

'the following selections are made en masse, ie not in any particular order 'there are approx. 3600 lines and 16 3dFaces msg = MsgBox("Select on screen all the TOP lines.", vbOKOnly) ssetObj1.SelectOnScreen msg = MsgBox("Select on screen all the TOP faces.", vbOKOnly) ssetObj2.SelectOnScreen

Dim LinLen As Variant Dim intPoints As Variant Dim TopArea As Variant Dim Area As Variant Dim entTopLine As AcadLine Dim entTopFace As Acad3DFace

'each of the 3600 lines are tested for intersection with a 3dFace For Each entTopLine In ssetObj1

'the 3dFaces are cycled thru to check for intersection with line For Each entTopFace In ssetObj2 On Error Resume Next intPoints = entTopLine.IntersectWith(entTopFace, acExtendThisEntity) 10 LinLen = intPoints(2) - 10 'error occurs here Area = LinLen * 0.25 TopArea = TopArea + Area Next Next ssetObj1.Delete ssetObj2.Delete

End Sub