MACRO in Excel - printing with sequential numbers

Asked

Viewed 3,351 times

5

I have a macro from a few years ago that still works, when I click to print the production orders of my company, I put the start and end number, and the macro prints the Ops with the numbers in order, follows the macro:

Sub PrintJobs()
Dim i As Long, startnum As Long, lastnum As Long

    startnum = Application.InputBox("Número de inicio", "Imprimir op", 1, , , , , 1)
    lastnum = Application.InputBox("Número de fim", "Imprimir op", 1, , , , , 1)

    For i = startnum To lastnum
        Range("G2").Value = i
        ActiveWindow.SelectedSheets.PrintOut
    Next

End Sub

(G2 in the code is the cell where the numbers will come out) the problem is that now I’m changing the ops to exit two per page (issue of saving leaves) half of the A4 comes out with an op, the other half comes out another op, I made a mirror in the spreadsheet for when change the first op, change also the second, making the two come out on the same sheet, only now my macro does not work as it should, because when the first op is 1, the second should be 2 and so on, I have tried to put the second op as the value of the first + 1, without success, can help me to change my macro, to G2 exit 1, G32 exit 2, then G2 exit 3, G32 exit 4, and so on. Thank you

  • 2

    Try it like this i = startnum Do While i < lastnum Range("G2").Value = i ActiveWindow.SelectedSheets.PrintOut i = i + 1 Range("G32").Value = i ActiveWindow.SelectedSheets.PrintOut i = i + 1 Loop

  • 1

    Hello @Marcosregis Thank you so much for the answer, I pasted your code and printed with example 1 to 4 , came out like this: first sheet, Folha1: op1 left with number 1, op 2 left with number 4 ; Folha2: left with op1 nº 1, op2 nº 2; leaf 3: op1 nº 3, op2 nº 2; leaf 4: op1 nº 3, op2 nº 4. Do you have any idea how to fix this? Thanks in advance & #Xa;

  • And how should it be? It would be easier to post a spreadsheet print and what event is it shooting at

  • @Marcosregis, thank you so much for your help, I just took a line of your code and it worked perfectly!! thank you very much! saved the day! i = startnum Do While i < lastnum Range("G2"). Value = i i = i + 1 Range("G32"). Value = i Activewindow.SelectedSheets.Printout i = i + 1 Loop

  • I will put then as solution, correct me if I get wrong.

1 answer

3


By your explanation I believe that instead of wearing a bow tie For be more suitable using a loop While

Try with this example.

Sub PrintJobs()
    Dim i As Long, startnum As Long, lastnum As Long

    startnum = Application.InputBox("Número de inicio", "Imprimir op", 1, , , , , 1)
    lastnum = Application.InputBox("Número de fim", "Imprimir op", 1, , , , , 1)

    i = startnum
    Do While i < lastnum 
        Range("G2").Value = i 
        i = i + 1
        Range("G32").Value = i 
        ActiveWindow.SelectedSheets.PrintOut
        i = i + 1 
    Loop
End Sub
  • Thank you very much Marcos, it worked perfectly!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.