about exiting vba loop

about exiting vba loop

ferhatttcoskun
Enthusiast Enthusiast
553 Views
1 Reply
Message 1 of 2

about exiting vba loop

ferhatttcoskun
Enthusiast
Enthusiast

Hello, I am giving a sequential number to the current article with the following codes.
But I want to exit before loop reaches 50.
I'm a newbie at this, can you help me please.

 

Private Sub CommandButton46_Click()

On Error Resume Next

Dim ek As String
ek = TextBox51.Text

For i = 1 To 50

Dim ent As AcadEntity
Dim yazi As AcadText
Dim myazi As AcadMText

ThisDrawing.Utility.GetEntity ent, pt, vbCrLf & "Düzeltilecek Yazıyı Seç:"
If TypeOf ent Is AcadText Then
Set yazi = ent
yazi.TextString = ek & TextBox52.Value
ElseIf TypeOf ent Is AcadMText Then
Set myazi = ent
myazi.TextString = ek & TextBox52.Value
Else
ThisDrawing.Utility.Prompt vbCrLf & "Geçersiz seçim.Lütfen yazı objesi seçin!" & vbCrLf
End If
TextBox52.Value = TextBox52.Value + 1

Next i

End Sub

Moderator edit: Put code into code window using </> button.

0 Likes
Accepted solutions (1)
554 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor
Accepted solution

Firstly, please post your code by using the toolbar button "</>" above the message window, so that the code is easily readable.

 

It seems you want user to keep select TEXT/MTEXT entities to update their TextString until user wants to stop. All CAD user knows to hits ESC key to stop data entering or entity/point/distance picking. So, in your code, if you do nothing, user can always hit Esc to stop, which may break your code, depending on the VBA setup (stop at all error, or only not handled error).

 

Also, it seems you pick an arbitrary number (50) to allow the loop goes to maximum 50 times, which is a very bad practice.

 

Following code shows how to do it correctly:

1. You use a do...loop while() to allow use to pick as many time as he/she wants;

2. in the picking method, also use a do...loop while() to ask user to pick at least once. User could hits ESC, or could enter keywork (to exit, in this case), or could select a wrong type of entity (e.g. not Text/MText). If first 2 cases, the code exits from loop and return a Nothing, meaning, user wants to stop the while thing. Only user selects a TEXT/MText, the code exit the loop and return a valid entity back, so the main  loop would update the selected TEXT/MTEXT; then go on for user to pick next TEXT/MTEXT.

 

Option Explicit

Public Sub UpdatePickedTextEntities()

    Dim ent As AcadEntity
    Dim entText As AcadText
    Dim entMText As AcadMText
    Dim text As String
    
    text = "XXXXXXXXXXXXXXXXXXXX"
    
    Do
        Set ent = SelectTextEntity()
        If ent Is Nothing Then Exit Do
        
        If TypeOf ent Is AcadText Then
            Set entText = ent
            entText.TextString = text
        ElseIf TypeOf ent Is AcadMText Then
            Set entMText = ent
            entMText.TextString = text
        End If
        
    Loop While (True)

End Sub

Private Function SelectTextEntity() As AcadEntity
    
    Dim ent As AcadEntity
    Dim pt As Variant
    Dim errDesc As String
    
    Dim keywords As String
    keywords = "eXit"
    
    On Error Resume Next
    
    ThisDrawing.Utility.InitializeUserInput 128, keywords
    
    '' use a loop, so that if select a wrong type of entity,
    '' we only loop back to ask user to re-seelct
    Do
        ThisDrawing.Utility.GetEntity ent, pt, vbCr & "Select TEXT/MEXT entity or [eXit]: "
        
        If Err Then
            errDesc = Err.Description
            If StrComp(errDesc, "User input is a keyword") = 0 Then
                ''User entered a keyword
                '' If there are multiple keyworkds, you would do
                '' something differently/accordingly
            Else
                '' User hits ESC, do something is neceaary
            End If
            '' in this particular case, whether use hits Esc, or enter the keywork
            '' we just want to stop the picking
            Exit Do
        End If
        
        If TypeOf ent Is AcadText Or TypeOf ent Is AcadMText Then Exit Do

    Loop While (True)
    
    Set SelectTextEntity = ent
    
End Function

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature