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:
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
CODE
BTW - what if your user enters "XYZ" as the answer to your InputBox
---- Andy
There is a great need for a sarcasm font.
RE: i + 1 loop with conditions
CODE
Skip,
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
RE: i + 1 loop with conditions
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
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
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
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
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
- 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
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
---- Andy
There is a great need for a sarcasm font.
RE: i + 1 loop with conditions
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
---- Andy
There is a great need for a sarcasm font.
RE: i + 1 loop with conditions
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
Thanks for the assistance!
Mike
RE: i + 1 loop with conditions
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
RE: i + 1 loop with conditions
CODE
'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
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
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
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
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
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
---- Andy
There is a great need for a sarcasm font.
RE: i + 1 loop with conditions
CODE
RE: i + 1 loop with conditions
Of course that line of code needs to be un-commented.
CODE
Skip,
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
RE: i + 1 loop with conditions
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
CODE
---- Andy
There is a great need for a sarcasm font.
RE: i + 1 loop with conditions
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
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
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
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
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
RE: i + 1 loop with conditions
Why do you want to say: Cancel, but run the macro anyway?
I would say, if cancel, then quit the macro:
CODE
---- Andy
There is a great need for a sarcasm font.
RE: i + 1 loop with conditions
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
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
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
Skip,
Just traded in my OLD subtlety...
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein