×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# i + 1 loop with conditions

## i + 1 loop with conditions

(OP)
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 

### RE: i + 1 loop with conditions

Just a guess here since your requirements are not very clear to me...

#### CODE

Sub multi_panel_2()

Dim i As Single
Dim x As Integer

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

If i >= 1 And i <= 120 Then
For x = i To 120
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
Next x
Else
MsgBox ("You entered Panel " & i & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub 

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

I think that the variable i needs to change, therefore...

#### CODE

Sub multi_panel_2()

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
For i = x To 120
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
Next x
Else
MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub 

Skip,

Just traded in my OLD subtlety...
for a NUance!

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

### RE: i + 1 loop with conditions

(OP)
Hi Skip,

As always, thank you for the assistance. When I run your code, I get an Invalid Next Control variable reference. I understand where you are going with it, but don't know how to fix the error.

Thanks,

Mike

### RE: i + 1 loop with conditions

Check the counter name, it should be the same as after Next.

Wouldn't be easier to create a simple userform with input textbox, dynamically filled multiselect listbox (can have rather ugly built-in checkboxes and depending on the developer either not selected or preselected all items), two buttons: first, 'update list' with value check and filling list, and second, 'go'? You could avoid up to 120 replies to "Are you ready to run file ...".

combo

### RE: i + 1 loop with conditions

In Skip's example:

#### CODE

...
Next x   '<- change x to i
... 

or in my example:

#### CODE

...
Dim i As Single    '<- change Single to Integer
Dim x As Integer
... 

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

(OP)
Hi combo,

The vb is going to be used in Mach3 as a macro for a CNC router. Mach3 doesn't support forms. I'm not 100% sure that this will work either but if it works in office,it'll be a good start.

Mike

### RE: i + 1 loop with conditions

You'll want to take the message box related code out of the loop.

Skip,

Just traded in my OLD subtlety...
for a NUance!

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

### RE: i + 1 loop with conditions

I'm not sure if this Cypress basic manual refers to the language you use, but if so:
- MsgBox type and buttons are coded in single [Type] parameter (pp. 100-102),
- vbOKCancel constant does not exist,
- you can build dialogs, there is no visual designer, but Begin Dialog ... End Dialog structure (pp. 60-61).

combo

### RE: i + 1 loop with conditions

Since this is Microsoft VBA for Applications forum, I would assume Mike is using something like Excel VBA
If so, his MsgBox could be written:

#### CODE

Select Case MsgBox("Are you ready to run file panel" & i & ".tap ?", vbOKCancel Or vbQuestion, "Ready?")
Case vbOK

Case vbCancel

End Select 

but - since this is a "Are you ready" question, I would expect Yes / No answers:

#### CODE

Select Case MsgBox("Are you ready to run file panel" & i & ".tap ?", vbYesNo Or vbQuestion, "Ready?")
Case vbYes

Case vbNo

End Select 

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

Andy, if you look at the manual I linked, you can see that it is 99% VB(A). Mach3 site states thet the language is the same as Cypress. It is not VBA however in this post: "The vb is going to be used in Mach3 as a macro for a CNC router. Mach3 doesn't support forms." Some structures are different and I suspect that the full code was not tested in real environment.
Much simpler if the code could run from a file created in VBA supporting application and use automation.

combo

### RE: i + 1 loop with conditions

Ooops! My fault. I missed the Mach3 part...

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

(OP)
Hi Folks,

Thanks for the assistance. Mach3 does support message boxes and all the other properties in the MS Office code. I did check.

My current code is working for the most part. There is one problem though. The NEXT loop will keep going as it should, but I need to do something a little different since during testing I ran across a situation that needs to be corrected.

Before each new file, I need to confirm that the operator wants to run it. I think the issue with the code is just an order of operation situation.

Steps:

1) Start macro
2) User enters the panel number into the message box prompt
3) A confirmation box is used to validate the starting point input value
4) The file is run (place holder continue)
5) the user's input number + 1
6) Verify that the user wants to run the next panel number (user's input + some value between 1 and 120)
7) The file is run
8) repeat till all files are run

What do I need to amend?

current code:

#### CODE

Sub multi_panel_3()

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 

Thanks for the assistance!

Mike

### RE: i + 1 loop with conditions

#### CODE

Sub multi_panel_3()

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

For i = x To 120
'Answer = Msgbox here to run the cnc
'If Answer = No Then Exit For
' 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 

Skip,

Just traded in my OLD subtlety...
for a NUance!

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

### RE: i + 1 loop with conditions

This makes a little more sense (to me, at least...) VBA approach, I hope it works the same in Mach3

#### CODE

Sub multi_panel_3()
'1) Start macro
Dim i As Integer
Dim x As Integer

'2) User enters the panel number into the message box prompt
x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x < 1 or x > 120 Then
MsgBox ("You entered Panel " & x & ".  Panel number is outside the range of 1 to 120.")
Exit Sub
End If

'3) A confirmation box is used to validate the starting point input value
If vbOK = MsgBox("Are you ready to run file panel" & x & ".tap" ?", vbOKCancel) Then
For i = x To 120
If vbOK = MsgBox("run " & x & ".tap ?", vbOKCancel) Then
' loadrun("c:\CNC\panel" & i & ".tap")
' code "M00"
End If
Next i
End If

End Sub 

'Starting point' question in MsgBox will be asked twice.
Do you really need to ask first time?

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

>I would assume Mike is using something like Excel VBA

Nah, if they are trying to script Mach 3, then they are using the Mach 3 scripting language, which is Cypress Enable, which is a VBA-alike - one that doers not appear to have any built-in predefined constants ... Combo has already linked to a relevant manual

### RE: i + 1 loop with conditions

>[Mach 3] doers not appear to have any built-in predefined constants
But it looks like vbOKCancel works OK for Mike...

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

We see the InputBox code result, no MsgBox ...

#### Quote (remeng)

Before each new file, I need to confirm that the operator wants to run it. I think the issue with the code is just an order of operation situation.

For MsgBox, if in Mach3 vbOKCancel=0, only OK button is displayed and 1 returned, 1=1, so we always enter the For...Next loop. According to the manual, the code should be:
confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", 1+32, "Confirm run")

combo

### RE: i + 1 loop with conditions

(OP)
Hi All;

I am still having the issue. Andrzejek code doesn't keep the NEXT loop going.

Again, I think it is an order of operation in my code.

Combo,

I don't understand what your last message meant. It might be just the formatting of the post. Could you please clarify?

Thanks,

Mike

### RE: i + 1 loop with conditions

Assumed you have Cypress VB script (you have not confirmed this). I am basing on the manual I linked to above.

If your script does not support VB/VBA named constans, in confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel) you have vbOKCancel=0, unless earlier you declared it as =1. If you pass o, the manual states 'Display OK button only.' Is it your case? If so, as MsgBox function returns the value of button clicked, you get confirm=1 (i.e. OK button selected, no other option). In next line you have If confirm = 1 Then, that is always True. The same refers to next lines and 'continue' testing.

You need to assign 1 to MsgBox Type argument if you plan to have OK and Cancel buttons. 32 is for question icon, not necessary, but in line with the message purpose.

combo

### RE: i + 1 loop with conditions

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.

### RE: i + 1 loop with conditions

(OP)
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 

### RE: i + 1 loop with conditions

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,

Just traded in my OLD subtlety...
for a NUance!

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

### RE: i + 1 loop with conditions

(OP)
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

### RE: i + 1 loop with conditions

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

#### 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
If 1 = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel) Then
If 1 = MsgBox("run " & x & ".tap to 120.tap", vbOKCancel) Then
For i = x To 120
' loadrun("c:\CNC\panel" & i & ".tap")
' code "M00"
Next i
End If
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 

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

(OP)
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

### RE: i + 1 loop with conditions

That "CALL" in my code was erroneous, copied from the last word of the previous comment. So sorry 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,

Just traded in my OLD subtlety...
for a NUance!

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

### RE: i + 1 loop with conditions

(OP)
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

### RE: i + 1 loop with conditions

#### 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,

Just traded in my OLD subtlety...
for a NUance!

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

### RE: i + 1 loop with conditions

(OP)
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 

### RE: i + 1 loop with conditions

> 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
If continue  = 1 Then
' loadrun("c:\CNC\panel" & i & ".tap")
' code "M00"
Else
Exit Sub
End If
Next i
... 

---- Andy

There is a great need for a sarcasm font.

### RE: i + 1 loop with conditions

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 'code "M00" 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,

Just traded in my OLD subtlety...
for a NUance!

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

### RE: i + 1 loop with conditions

(OP)
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:

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.

### RE: i + 1 loop with conditions

Well have you done a test run with my code?

Skip,

Just traded in my OLD subtlety...
for a NUance!

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

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!