how to execute the command after "If-then" is executed or before it is executed

Garagal

New Member
Joined
Nov 2, 2022
Messages
3
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello, I would like to ask how to execute a command after the "if-then" is fulfilled, or before the "if-then" is executed.

VBA Code:
'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"

Here's the first condition:
VBA Code:
 If po.Range("M4") + 2 > po.Range("O6") Then po.PageSetup.PrintArea = "$B$1:$L$43"
I want this command to be executed in order,such as, after changing the area print then the next "exit for" command will work so that it does not coincide with the execution of the printarea change.

The second condition is almost the same as the first condition but I'm confused about how this command can read ahead without waiting for the .printpreview change, at
VBA Code:
If IsError(po.Range("C4")) Then Exit For

in this command if "C4" becomes N/A or other then the loop will stop, but when I run it this command always executes after .printpreview runs so I need to discard 1 paper containing the value N/A.

can both conditions of my question be solved? if possible please help me thank you.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Here's the full code if needed
VBA Code:
'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
 
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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