Home > Blockchain >  How to Execute command/macro, before "If-then" applied or after "If-then" applie
How to Execute command/macro, before "If-then" applied or after "If-then" applie

Time:11-05

'Membuat shortcut sheet untuk lebih pendek
'Make shortcut
Set po = Sheets("Print Out")
Dim awal, Akhir As Integer
Dim j As Long
'mencantumkan N6 & O6 menjadi value acuan
'Make N6 & O6 as main value print
awal = po.Range("N6").Value
Akhir = po.Range("O6").Value
j = 0
'menjalankan menu pilih printer
'Show up print dialog and set "normal" print area (w/o) condition
Application.ScreenUpdating = False
Application.Dialogs(xlDialogPrinterSetup).Show
po.PageSetup.PrintArea = "$B$1:$L$57"
'perintah utama
'Main command to apply auto mass print
    For i = awal To Akhir
        With po
            .Range("M1").Value = i   0   j
            .Range("M2").Value = i   1   j
            .Range("M3").Value = i   2   j
            .Range("M4").Value = i   3   j
            .PrintPreview
            j = j   3
        End With
'jika mendeteksi N/A atau sejenisnya perintah akan berhenti
'If found #N/A or #REF or any error loop will stop to prevent loop printing even data already reach limit
      If IsError(po.Range("C4")) Then Exit For
      If IsError(po.Range("C18")) Then
      po.PageSetup.PrintArea = "$B$1:$L$15"
      Exit For
'Jika M4 lebih besar dari batas akhir (O6) maka perintah akan terhenti
'If m4 > than O6 then print area will change and stop the loop (THIS IS THE PROBLEM)
      If po.Range("M4")   2 > po.Range("O6") Then po.PageSetup.PrintArea = "$B$1:$L$43"
          If po.Range("M3")   3 > po.Range("O6") Then po.PageSetup.PrintArea = "$B$1:$L$29"
            If po.Range("M2")   4 > po.Range("O6") Then po.PageSetup.PrintArea = "$B$1:$L$15"
    Next i

The macro I created as above, but there is a problem that I cannot overcome.

Like the following line:

If po.Range("M4") 2 > po.Range("O6") Then po.PageSetup.PrintArea = "$B$1:$L$43"

Basically, the code above will adjust the "printarea" if it has crossed the limit I have set, but I want to add an "Exit for" command to stop the loop after the "If-then" criteria is met.`

I already tried to make

If po.Range("M4")   2 > po.Range("O6") Then 
po.PageSetup.PrintArea = "$B$1:$L$43
Exit for

but the "Exit for" code will run along with the printarea change, so my last page has not been printed yet. What I want is for the loop to stop after my last page is printed (where my last page will always be related to the "if then" above).

CodePudding user response:

It looks like you miss the ELSE statement and END IF.

If po.Range("M4")   2 > po.Range("O6") Then 
    po.PageSetup.PrintArea = "$B$1:$L$4"
Else
    Exit for
End if

CodePudding user response:

You can also use GOTO to get your program to continue from another place in your code. EXIT FOR will always forse your FOR loop to end.

If po.Range("M4")   2 > po.Range("O6") Then 
    po.PageSetup.PrintArea = "$B$1:$L$4"
Else
    GOTO ContinueHere 
End if
' Code you want to skip

ContinueHere:
' Continues to code here  

NEXT
  • Related