Exiting a loop

Exiting a loop

anishkt
Contributor Contributor
2,408 Views
5 Replies
Message 1 of 6

Exiting a loop

anishkt
Contributor
Contributor

Hi,

I have a VBA code to select a number of objects and to copy the parameters to excel. I need to be able to exit the loop when no object is selected or by right click or esc. I have done this is lisp. But cannot figure out a way in VBA. Can someone help.

Regards,

Anish

0 Likes
2,409 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

If you show your code along with the question, people would not have to play the guess game to know if they know the answer or not.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

anishkt
Contributor
Contributor

Thank you Norman. here is the code..

 

"Do While Length > 50
Set myXL = GetObject(, "Excel.Application")
Rebar.color = acCyan
Set shtObj = myXL.Worksheets(1)
leng = Format(Length, "###,##0")
shtObj.Cells(i, j).Value = leng
j = j + 1
ThisDrawing.Utility.GetEntity Rebar, Pickedpoint, vbCr & "Select Reinforcement."
Rebar.Update
Length = Rebar.Length
Loop"

I want to exit the loop after I have finished selecting the required lines. this can be by making a null selection or by right clicking or pressing esc. Since I was not able to do this, I made a condition to exit the loop if the length of line is less than 50.

Hope this is clear.

0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor

I ma not sure the reason you need a Do...Loop to surround the user input (AcadUtility.Getxxxx()).

 

When getting a user input, the result could be either expected, or not expected (invalid input), or cancel (when user press Esc key, which results in VBA to raise a runtime error).

 

Usually a loop could be used to surround GetXXXX() call, if the user input is not in expected range and you want user to input again; or if the user input presents a list of keywords, some of them result in another round of user input. 

 

In your case, if you indeed required the selected entity being longer that 15, then your code does things correctly: user will be asked for input again and again until the selected entity is longer than 15. However, it may be better to use keyword  (AcadUtility.InitializeInput()) in conjunction with GetXxxx() call, to give user other necessary/better options.

 

Also, when using GetXXXX(), it is common practice to handle error to catch user pressing Esc button to cancel the input:

 

On Error Resume Next

ThisDrawing.Utility.GetEntity ent, pt, vbCr & "Select entity:"

 

In the next line of code, you can either test if the "ent" is Nothing or not. If it is nothing, then user pressed Esc; or you can test err.Number:

 

If err.Number<>0 Then

  ''User pressed Esc

End if

 

If you do use Loop to surround the GetXxxx() call, you can exit the loop anytime (in whatever condition you choose) when Exit Loop statement:

 

On Error Resume Next

Do 

 

    ThisDrawing.Utility.GetXxxx ...

 

    ''Test error (user pressed Esc?)

 

    '' Do something

 

    If [condition2] Then

        Exit Loop

    End if

 

    '' Loop continues as long as [condition1] is met

 

Loop While [condition1]

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 6

Anonymous
Not applicable

I guess you probably want the function below codes

 

Option Explicit

 

Public Sub test()

 

Dim length As Integer
Dim myXL As Excel.Application
Dim Rebar As AcadObject
Dim shtObj As Worksheet
Dim leng As String
Dim i, j As Integer
Dim Pickedpoint As Variant

 

Set myXL = GetObject(, "Excel.Application")

i = 1: j = 1

 

Do While True

ThisDrawing.Utility.GetEntity Rebar, Pickedpoint, vbCr & "Select Reinforcement."

length = Rebar.length

If length <= 50 Then Exit Do

Rebar.color = acCyan

Set shtObj = myXL.Worksheets(1)

leng = Format(length, "###,##0")

shtObj.Cells(i, j) = leng

j = j + 1

Loop

End Sub

 

 

Message 6 of 6

anishkt
Contributor
Contributor

thank you Norman and Shackle.

I was able to get what I needed by "Do while err.number<>0" and adding an "on error resume next" before the getxxxx.

 

The loop is required because I need to transfer the length of the lines selected to excel until all selections are made.

the criteria >50 was kept because I did not know how to code for exit when no selection, esc or right click was made.

 

Now it is sorted out.

 

Thank you very much for your valuable input.

0 Likes