[VBA][VBA]请斑竹帮助调试
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">' </FONT>坐标点排序函数<BR><FONT face="Times New Roman">' </FONT>语法:<FONT face="Times New Roman">SortPoints(Points, SortMode)<BR>' Points</FONT>为坐标点数组<BR><FONT face="Times New Roman">' SortMode</FONT>为排序方式:<FONT face="Times New Roman">0=X</FONT>向,<FONT face="Times New Roman">1=Y</FONT>向,<FONT face="Times New Roman">2=Z</FONT>向<BR><FONT face="Times New Roman">' </FONT>返回值为排序后的坐标点数组<BR><FONT face="Times New Roman">Public function SortPoints(Points As Variant, SortMode As String) As Variant<BR> Dim NewPoints() As Variant<BR> ReDim NewPoints(UBound(Points))<BR> Dim k As Long<BR> For k = 0 To UBound(NewPoints)<BR> NewPoints(k) = Points(k)<BR> Next k<BR> <BR> Dim BestPoint As Variant<BR> Dim Pnt1 As Double<BR> Dim Pnt2 As Double<BR> Dim i As Long<BR> Dim j As Long<BR> Dim Best_Value As Double<BR> Dim Best_j As Long<BR> For i = 0 To UBound(NewPoints) - 1<BR> Best_Value = NewPoints(i)(SortMode)<BR> BestPoint = NewPoints(i)<BR> Best_j = i<BR> For j = i + 1 To UBound(NewPoints)<BR> If NewPoints(j)(SortMode) < Best_Value Then<BR> Best_Value = NewPoints(j)(SortMode)<BR> BestPoint = NewPoints(j)<BR> Best_j = j<BR> End If<BR> Next j<BR> NewPoints(Best_j) = NewPoints(i)<BR> NewPoints(i) = BestPoint<BR> Next i<BR> SortPoints = NewPoints<BR>End Function ' </FONT>示例<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"> <o:p></o:p>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Sub FindLineStartPoint()</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim Sel As AcadSelectionSet</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">On Error Resume Next<BR>Set Sel = ThisDrawing.SelectionSets.Add("ssel")<BR>If Err Then<BR> Err.Clear<BR> ThisDrawing.SelectionSets("ssel").Delete<BR> Set Sel = ThisDrawing.SelectionSets.Add("ssel")<BR>End If<BR>On Error GoTo 0</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman"> </FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">'</FONT>选线<BR><FONT face="Times New Roman">Sel.SelectOnScreen</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">'</FONT>获取坐标点<BR><FONT face="Times New Roman">Dim obj As AcadObject<BR>Dim i As Integer<BR>Dim pa(300) As Variant<BR>Dim str As String<BR>i = 0<BR>str = "</FONT>未排序坐标<FONT face="Times New Roman">"<BR>For Each obj In Sel<BR> If obj.ObjectName = "AcDbLine" Then<BR> pa(i) = obj.StartPoint<BR> str = str & Chr(13) & " </FONT>第<FONT face="Times New Roman">" & i + 1 & "</FONT>点坐标为<FONT face="Times New Roman">: ""( " & pa(i)(0) & "," & pa(i)(1) & _</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman"> "," & pa(i)(2) & ")" & Chr(13)<BR> i = i + 1<BR> End If<BR>Next<BR>MsgBox str</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">'</FONT>排序<BR><FONT face="Times New Roman">Dim pb As Variant<BR>pb = SortPoints(pa, 0)</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">newtxt = </FONT>按<FONT face="Times New Roman">X</FONT>坐标点排序<FONT face="Times New Roman">"<BR> For i = 0 To UBound(pa)<BR> newtxt = newtxt & vbCr & </FONT>第<FONT face="Times New Roman">" & i + 1 & "</FONT>点坐标为<FONT face="Times New Roman">:" & pb(i)(0) & _<BR> " " & pb(i)(1) & " " & pb(i)(2)<BR> Next<BR> MsgBox oldtxt & vbCr & vbCr & newtxt, , "</FONT>明经通道<FONT face="Times New Roman">VBA</FONT>示例<FONT face="Times New Roman">"<BR>Sel.Delete<BR>End Sub</FONT>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman"></FONT>请斑竹帮助调试,屏幕选取直线获得起始点坐标数组,数组排序,<FONT face="Times New Roman"> If NewPoints(j)(SortMode) < Best_Value Then</FONT>这一句有问题 我把代码贴到vba编辑器中,红色的出错行很多啊。
楼主不是直接复制代码上来的? SortPoints函数没有问题,问题出在传递给函数的变量pa上,pa应该是一个点的数组,但你直接声明了pa(300),但你选择的对象一般少于300,假定是N,那么pa的下标在N+1~300之间都没有定义,这样就不存在pa(j)(sortmode)。在主过程中把Dim pa(300) As Variant改为Dim pa() As Variant
k = Sel.Count
ReDim pa(k - 1)下面是我修改后的主过程,测试没有问题。 Sub FindLineStartPoint()Dim Sel As AcadSelectionSetOn Error Resume Next
Set Sel = ThisDrawing.SelectionSets.Add("ssel")
If Err Then
Err.Clear
ThisDrawing.SelectionSets("ssel").Delete
Set Sel = ThisDrawing.SelectionSets.Add("ssel")
End If
On Error GoTo 0'选线
Sel.SelectOnScreen'获取坐标点
Dim obj As AcadObject
Dim i As Integer
Dim pa() As Variant
k = Sel.Count
ReDim pa(k - 1)
Dim str As String
Dim k As Integer
i = 0
str = "未排序坐标"
For Each obj In Sel
If obj.ObjectName = "AcDbLine" Then
pa(i) = obj.StartPoint
str = str & Chr(13) & " 第" & i + 1 & "点坐标为: ""( " & pa(i)(0) & "," & pa(i)(1) & _
"," & pa(i)(2) & ")" & Chr(13)
i = i + 1
End If
Next
MsgBox str'排序
Dim pb As Variant
pb = SortPoints(pa, 0)newtxt = "按X坐标点排序"
For i = 0 To UBound(pa)
newtxt = newtxt & vbCr & "第" & i + 1 & "点坐标为:" & pb(i)(0) & _
" " & pb(i)(1) & " " & pb(i)(2)
Next
MsgBox oldtxt & vbCr & vbCr & newtxt, , "明经通道VBA示例"
Sel.Delete
End Sub
谢谢 <A name=5229><FONT color=#000066><B>subtlation</B></FONT></A>
我编程没几天,又很多基本问题还没没搞清楚。
------------
学习、学习、再学习!
页:
[1]