Excel VBA for running consecutive loops

averan

New Member
Joined
Jul 29, 2019
Messages
1
I have created an excel userform to record pallets of rejected product on a production line. The user fills in details and prints out a label to insert on the pallet of rejected product. On occasion, the production line may be able to reuse rejected product within the production area and there is a checkbox on my form which the user ticks if this is the case. The code then makes some extra textboxes visible and prints a label as before.

My code works for the straightforward case where one pallet is rejected and is to be reused. Where I encounter problems is if, for example, 2 pallets of rejects are created but only one pallet is to be reused and the other to be scrapped. I want to print one label without the extra textboxes and one label with said textboxes.

The code below is not giving me any error messages but it is not printing when the case is = true and NoCopies (textbox) > 1.

I'm self-taught when it comes to VBA so may be going at this completely the wrong way and I'm not sure where to go next as I have tried a number of things e.g. two consecutive for loops : For p = 1 to p = prnt then For p = prnt+1 to NoCopies.Value and similar Do Until statements with no joy - either nothing prints or only one of the For statements run. I get no error messages. Any help to point me in the right direction is greatly appreciated

Code:
Private Sub checkandprint()
Dim p As Integer
Dim prnt As Variant

'print set number of copies, set background to white and make save and print button invisible

 CommandButton1.Visible = False
  Complex_ETicket_pt4.BackColor = vbHighlightText
 
 For i = 1 To 11
    Controls("Label" & i).BackColor = vbHighlightText
 Next i
 
 
Select Case CheckBox1.Value

  Case Is = False

   CheckBox1.Visible = False
   Label11.Visible = False
   Spraybox.Visible = False
  
   For p = 1 To NoCopies.Value
    Me.ScrollBars = fmScrollBarsNone
    Complex_ETicket_pt4.PrintForm
   Next p
  
 'if there is a multi print and they are reusing, find out
 'how many pallets are being reused and print/spray the correct details

    Case Is = True And NoCopies.Value > 1
   
     prnt = Round(Application.InputBox("How many pallets are to be used within the cell?", Type:=1), 0)
 
       If NoCopies.Value <> prnt Then
      
        For p = 1 To p = NoCopies.Value
       
          While p <= prnt

         
           CheckBox1.Visible = True
           CheckBox1.BackColor = vbHighlightText
           Label11.Visible = True
           Spraybox.Visible = True
            If p > 1 Then
            Spraybox.Value = Spraybox.Value + 1
            End If
           Me.ScrollBars = fmScrollBarsNone
           Complex_ETicket_pt4.PrintForm
 
         Wend

        
         While p > prnt
 
          CheckBox1.Visible = False
          Label11.Visible = False
          Spraybox.Visible = False
          Me.ScrollBars = fmScrollBarsNone
          Complex_ETicket_pt4.PrintForm
 
         Wend

         
        Next p

       
       Else

           CheckBox1.Visible = True
           CheckBox1.BackColor = vbHighlightText
           Label11.Visible = True
           Spraybox.Visible = True
           Me.ScrollBars = fmScrollBarsNone
           Complex_ETicket_pt4.PrintForm

       End If
 
   
      Case Is = True And NoCopies.Value = 1
 
           CheckBox1.Visible = True
           CheckBox1.BackColor = vbHighlightText
           Label11.Visible = True
           Spraybox.Visible = True
           Me.ScrollBars = fmScrollBarsNone
           Complex_ETicket_pt4.PrintForm
 
End Select

 
 CommandButton1.Visible = True

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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