Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i + 1 loop with conditions

Status
Not open for further replies.

remeng

Technical User
Jul 27, 2006
520
US
Hi All;

I am sure that this is an easy one, but I forgot how to finish this code off.

The goal is for a user to enter a starting number (i) between 1 to 120. If the number is within the range of 1 to 120, it runs the IF statement. Once the IF statement is run, I'd like to perform a Next i operation. The code would end once i is greater than 120. The problem is where to put the For and Next statements.

Here is my code:

Code:
Sub multi_panel_2()

Dim i As Single

i = InputBox("What panel number would you like to start with?", "Starting Panel Number")


    If i >= 1 And i <= 120 Then
    
         confirm = MsgBox("Are you ready to run file " & "panel" & i & ".tap" & " ?", vbOKCancel)

            If confirm = 1 Then
        
                MsgBox ("run " & i & ".tap") ' placeholder for file call
            
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"
              

            Else: End If 'place holder for  code "M30" End If
        
        

    Else: MsgBox ("You entered Panel " & i & ".  " & "Panel number is outside the range of 1 to 120.")
    
        End If


End Sub
 
It would help if you would show your actual code, so we would know what you have at this moment.


---- Andy

There is a great need for a sarcasm font.
 
Here you go Andy. This is my last personal version before any recommended changes. As mentioned, it gets hung up at the Next I statement. There aren't any errors though.

Code:
Sub multi_panel_3() ''' Historic

Dim i As Integer
Dim x As Integer

x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x >= 1 And x <= 120 Then
    
        confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel)

        If confirm = 1 Then
        
            
            
            continue = MsgBox("run " & x & ".tap", vbOKCancel)  ' placeholder for file call
                
            If continue = 1 Then
            
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"
            For i = x To 120
            
            Next i
            
            End If
            
        Else: End If 'place holder for  code "M30" End If

Else
    MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub
 
Your loadrun statement was not in the loop!
Of course that line of code needs to be un-commented.
Code:
'
                
            If continue = 1 Then
            

            For i = x To 120
                 ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"           
            Next i
            
            End If

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Hi Skip,

I though that CALL referenced a second macro. If I put a placeholder message, would that be a temp way to get around the MACH3 command line?

Thanks,

Mike
 
My version of your code (very little difference), re-aligned, easier to read (IMO [upsidedown])

Code:
Sub multi_panel_3() [green]''' Historic[/green]

Dim i As Integer
Dim x As Integer

x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x >= 1 And x <= 120 Then
    If 1 = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel) Then
        If 1 = MsgBox("run " & x & ".tap [highlight #FCE94F]to 120.tap[/highlight]", vbOKCancel) Then
            For i = x To 120[green]
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"[/green]    
            Next i
        End If     
    End If [green]'place holder for  code "M30" End If[/green]
Else
    MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub


---- Andy

There is a great need for a sarcasm font.
 
Hi Andy,

Unfortunately this is for a CNC router so a physical part needs to be changed from file to file and the message boxes are needed. I need a message box to pop up for 2 reasons. 1, it allows the operator to confirm the correct file will be run next. 2, it allows for the machine to pause until the part is changed over from the machined part to the new part blank.

I hope this helps explain my thought process. If there wasn't a reason for the stops and message boxes, your code would work perfectly.

Any ideas now on what I can do to create the message box for the next part run confirmation to pop up before the NEXT statement (i+1)?

Thanks,

Mike
 
That "CALL" in my code was erroneous, copied from the last word of the previous comment. So sorry [blush] I failed to check my post before sending it off into the netosphere.

Bottom line: the code you want to actually run iteratively, must be within the For...Next loop.


Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Hi Skip,

No worries. I do that all the time. Could you post the code so I can review what you are educating me on? Everyone's code is all over the place.

Thanks for the help,

Mike
 
Code:
'
                
            If continue = 1 Then
            

            For i = x To 120
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"           
            Next i
            
            End If

Of course, you'll need to un-comment the loadrun statement.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Hi Skip,

I have one more issue. Here is my updated code. For the second continue MSGBOX, if it equals cancel, how do I run the M30 code and then quit the macro?

Code:
Sub multi_panel_5()

Dim i As Integer
Dim x As Integer

x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x >= 1 And x <= 120 Then
    
        confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel)

        If confirm = 1 Then
        
            
            
            continue = MsgBox("run " & x & ".tap", vbOKCancel)  ' placeholder for file call
                
            If continue = 1 Then
            
                ' loadrun("c:\CNC\panel" & i & ".tap")
                
                ' code "M00"
            
    
            
            For i = x To 120
            
                continue = MsgBox("run " & i & ".tap", vbOKCancel)  ' placeholder for file call
            
                ' loadrun("c:\CNC\panel" & i & ".tap")
                
                ' code "M00"
            
            Next i
            
            End If
            
        Else: End If 'place holder for  code "M30" End If

Else
    MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub
 
> if it equals cancel, how do I run the M30 code and then quit the macro?
Why do you want to say: Cancel, but run the macro anyway?
I would say, if cancel, then quit the macro:

Code:
...
For i = x To 120
    continue = MsgBox("run " & i & ".tap", vbOKCancel)  ' placeholder for file call[blue]
    If continue  = 1 Then  [/blue]
        ' loadrun("c:\CNC\panel" & i & ".tap")
        ' code "M00"[blue]
    Else
        Exit Sub
    End If    [/blue]
Next i
...


---- Andy

There is a great need for a sarcasm font.
 
Now we are guessing at what your logic is based on your code, which may not be what you actually want. Have no idea what [highlight #FCE94F]'code "M00"[/highlight] is supposed to mean.

As an analyst, I'd sit down with you and ask what the logical flow of your process was, where the decision points in that process are. I'd ask those questions in different ways in order be certain I understood your requirements.
Code:
Sub multi_panel_5()

    Dim i As Integer
    Dim x As Integer
    
    x = InputBox("What panel number would you like to start with?", "Starting Panel Number")
    
    If x >= 1 And x <= 120 Then
        
            confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel)
    
            If confirm = 1 Then
                
                For i = x To 120
                
                    continue = MsgBox("run " & i & ".tap", vbOKCancel)  ' placeholder for file call
                
                    If continue = 1 Then
                        ' loadrun("c:\CNC\panel" & i & ".tap")
                        
                        ' code "M00"
                    Else
                        ' code "M00"
                        Exit For
                    End If
                
                Next i
            Else
                'code "M30"
            End If
    
    Else
        MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
    End If

End Sub

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Hi Skip,

The code is for Mach3 which is running my CNC router. I need to run different milling codes that use the same physical fixture. Each file (.tap) has a unique milling pattern.

I have 120 different files to run in sequential order.

MACH3 Codes and what they mean:

* Code M00 is the MACH3 software command telling the CNC router to start the motor and run the routine <- Need to confirm this but a different code would still use the same type of call out.
* Code M30 is the Mach3 software command telling the CNC router to turn off the motor and end the .tap file

Brief article on general G-Code: G-Code explaination


The CNC router:

CNC_xr5env.jpg


Here is the general process:

1) The worker starts the VB macro in MACH3
2) The worker enters the file number they want to start with in a message box (1 thru 120; ok / cancel; if cancel run Code M30 and end macro)
3) The file name is confirmed in a new message box (ok / cancel; if cancel end macro)
4) When OK is pressed, the MACH3 file will open and run the G-Code (.tap file) then Code M00.
5) Once the .tap file finishes, i + 1 will bring up the next file. I want a new message box to confirm the file (i + 1); ok open i + 1 .tap file and run Code M00 / cancel; if cancel run Code M30 end macro

I used message boxes in some areas as placeholders since I don't want to test with .tap files until I can confirm that the macro is functioning correctly.

I hope this makes it more clear.
 
Well have you done a test run with my code?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top