Print range based on cell value

EssKayKay

Board Regular
Joined
Jan 5, 2003
Messages
221
Office Version
  1. 2007
Platform
  1. Windows
I’m looking for a way (I assume VBA code) to only print rows where column B is displaying data (i.e., B<>””). All cells in column B have a formula in them but depending on data input, many will appear blank; therefore, the number of applicable rows varies. That is, sometimes I may want to print B30:K35, other times it may be as large as B30:K777. The entire potential print range is B30:K1000.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try with this


Code:
Sub Macro11()
'
    ActiveSheet.PageSetup.PrintArea = ""
    uc = Range("B30").SpecialCells(xlLastCell).Column
    i = 30
    Do While Cells(i, "B") <> ""
        i = i + 1
    Loop
    rng = Range("A30", Cells(i - 1, uc)).Address
    ActiveSheet.PageSetup.PrintArea = rng
    ActiveSheet.PrintOut
End Sub
 
Upvote 0
Thanks for your response Dante. I'm receiving a Compile Error "Variable not defined for (UC =)". Do I need to define the entire potential print range (i.e., B30:K1000)?
 
Last edited:
Upvote 0
Just need to declare variables

Try this:

Code:
Sub Macro11()
'
    Dim i As Double, rng As String
    ActiveSheet.PageSetup.PrintArea = ""
    i = 30
    Do While Cells(i, "B") <> ""
        i = i + 1
    Loop
    rng = Range("B30:K" & i - 1).Address
    ActiveSheet.PageSetup.PrintArea = rng
    ActiveSheet.PrintOut
End Sub
 
Upvote 0
Just need to declare variables

Try this:

Code:
Sub Macro11()
'
    Dim i As Double, rng As String
    ActiveSheet.PageSetup.PrintArea = ""
    i = 30
    Do While Cells(i, "B") <> ""
        i = i + 1
    Loop
    rng = Range("B30:K" & i - 1).Address
    ActiveSheet.PageSetup.PrintArea = rng
    ActiveSheet.PrintOut
End Sub

Dante,

It prints however it prints nothing. Here is a screenshot of my worksheet. In this case, I only want 10 lines to print. The last line should be Row 42.

I'm trying to include a screenshot but this is not letting me.

Sorry,
SKK
 
Upvote 0
Got it. I noted row 30 as start of print range. It should have been 33. Rows 30-32 are a header. I changed the code to read 33 and all appears to be fine.

Thank you very much Dante - much appreciated,
Steve
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,214,524
Messages
6,120,049
Members
448,940
Latest member
mdusw

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