Do Until Loop

Tropez

New Member
Joined
Apr 19, 2004
Messages
35
Greetings all, I have a form I've created that automatically increments ticket numbers. What I'm trying to achieve is the user opens the form and clicks on a button to "Print Tickets."

A form opens:
Code:
frmticketpages.Show

What I'm trying to achieve is from the form, the user is asked how many pages of tickets to print out. The response is typed into a textbox and when the OK button is clicked:
Code:
Private Sub cmdok_Click()
Dim PagesToPrint As Integer

    Do Until ActiveCell.Value = 0
        Application.Goto Reference:="printpages"
        ActiveCell.Value = txtnoofpages.Value
        ActiveCell.Value = PagesToPrint
        MsgBox = PagesToPrint.Value
        If PagesToPrint > 0 Then
        '    Run "Print_Me"
        Run "NextTicketNum"
        Run "CopyTicketNum"
        Application.Goto Reference:="printpages"
        PagesToPrint = PagesToPrint - 1
        Else
    Loop
    
    Unload Me

End Sub

Basically each time a print is done I want to lower the number "PagesToPrint" variable by one, autoincrement the numbers by 5 (NextTicketNum) and then rerun the print command. This loops until the "PagesToPrint" cell is at 0.

Any suggestions on how to better do this or fix my code?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This is what I think you are doing... The form you are using has a textbox called txtnoofpages. In your code you have a reference to a cell (printpages). Trying to use the cell as a holder for the number of pages to print. But each time you go through the loop you are changing the value in the cell (printpages) to txtnoofpages and then changing the cell value to PagesToPrint. 1) you are never changing txtnoofpages. 2) You never set PagesToPrint.

You can use the following code and I think it should work...

Code:
Private Sub cmdok_Click()
Dim x as Integer

    For x = txtnoofpages.Value to 1 Step - 1
        MsgBox x
        'Run "Print_Me" 
        Run "NextTicketNum" 
        Run "CopyTicketNum"
    Next x

    Unload Me 

End Sub
 
Upvote 0
YES

That got it done!!

Do you mind if I ask why you use the "For Next" command instead of the "Do Until Loop" as I thought would have made it work?

Thanks again!
Ken
 
Upvote 0
The "For Next" and "Do Until Loop" are looping structures...

In the "Do Until Loop" you are using some criteria to test if the loop should continue... It would work in your case and is good when you do not know exactly how many times you want to execute the loop.

I used the "For Next" because you knew exactly how many times to execute the loop...
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top