Printing Pages based on cell contents Query

5h1v

Board Regular
Joined
Oct 11, 2012
Messages
69
Hi,

I have a worksheet that is set up to 8 print areas I think I have made a macro that should only print each page if the first cell in it has contents but I cant seem to get it to work, would you be so kind as to have a look please, apologies if it's wrong I'm pretty new to VBA.

Code:
Sub Printing()
'
' Printing Macro
'
  If IsEmpty(Range("b2").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1
End If
   If IsEmpty(Range("b52").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=2, Copies:=1
End If
    If IsEmpty(Range("b102").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=3, Copies:=1
End If
  If IsEmpty(Range("b152").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=4, Copies:=1
End If
    If IsEmpty(Range("b202").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=5, Copies:=1
End If
    If IsEmpty(Range("b252").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=6, Copies:=1
End If
  If IsEmpty(Range("b302").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=7, Copies:=1
End If
  If IsEmpty(Range("b352").Value) = True Then
        Cancel = True
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=8, Copies:=1
End If
End Sub

Thanks for the help
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi, i'm also new in VBA :)
Your script looked like a nice base, so i worked on it and i made it work for my case and it looks to me that you have same problem


chages that i made are:


- I added ELSE to your IF statement
- range of pages in every IF statement



Sub Printing()
'
' Printing Macro
'
If IsEmpty(Range("G11").Value) = True Then
Cancel = True
Else
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1
End If
If IsEmpty(Range("G40").Value) = True Then
Cancel = True
Else
ActiveWindow.SelectedSheets.PrintOut From:=2, To:=2, Copies:=1
End If
If IsEmpty(Range("G69").Value) = True Then
Cancel = True
Else
ActiveWindow.SelectedSheets.PrintOut From:=3, To:=3, Copies:=1
End If


End Sub




i have three pages on a worksheet and based on value of the cells (G11=1; G40=null; G69=1) i get printed out pages 1 and 3.


hope it helps


Also found another one that works, but this one is better for me because i can check sheet even if it is not active. You can speify sheet in the 4th line (name of my sheet = print)
Set wkspodinvoice = ThisWorkbook.Worksheets("print")


Option Explicit
Sub Print_Parameters()


Dim wkspodinvoice As Worksheet
Set wkspodinvoice = ThisWorkbook.Worksheets("print")


With wkspodinvoice
If .Range("G11").Value > 0 Then .PrintOut From:=1, To:=1, Copies:=1
If .Range("G40").Value > 0 Then .PrintOut From:=2, To:=2, Copies:=1
If .Range("G69").Value > 0 Then .PrintOut From:=3, To:=3, Copies:=1
End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,773
Members
449,336
Latest member
p17tootie

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