Debug VBA for dynamic print range

jrc999

New Member
Joined
Jan 5, 2018
Messages
7
I am putting together a template file that users will copy sheets into, and run macros to create reports. I'm trying to use VBA to set the print range for a pivot table, when the number of rows and columns can be different for each report created (i.e., each time the template is used).

I'm getting a Run-time error '1004' The formula you typed contains an error on this line:

ActiveSheet.PageSetup.PrintArea = "BHPivot!R1C1:R" & lrow& "C" & lcol

The code is as follows:

Rich (BB code):
        Dim lcol As Long
        Dim lrow As Long
        lrow =Sheets("BHPivot").Range("a" & Rows.Count).End(xlUp).Row
        lcol =Sheets("BHPivot").Cells(lastrow, Columns.Count).End(xlToLeft).Column
       ActiveSheet.PageSetup.PrintArea ="BHPivot!R1C1:R" & lrow & "C" & lcol


I adapted this from code I used to set a dynamic pivot table range, and it works perfectly for that use. Just can't figure out why it's giving an error here.

Thanks so much!


<strike></strike>
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

Try converting your print range into "A1" reference style:

Code:
    Dim lcol            As Long
    Dim lrow            As Long
    Dim sPrint          As String
    
    lrow = Sheets("BHPivot").Range("a" & Rows.Count).End(xlUp).Row
    lcol = Sheets("BHPivot").Cells(lrow, Columns.Count).End(xlToLeft).Column
    sPrint = Application.ConvertFormula("R1C1:R" & lrow & "C" & lcol, xlR1C1, xlA1)
    ActiveSheet.PageSetup.PrintArea = "BHPivot!" & sPrint

I also noticed that in your lcol command you are referring to "lastrow", instead of "lrow".

I hope it helps.
 
Upvote 0
Hugely helpful--that's a great solution! And thank you so much for catching that error.

Appreciate your help so much!!!
 
Upvote 0
jrc,
Re: setting print areas with VBA

Print area requires an A! style reference. Also, "lastrow" looks suspicious.
This works for Sheet1...
'---
Sub Revised()
Dim lcol As Long
Dim lrow As Long
Dim strWhere As String

With Sheets("Sheet1") ' or "BHPivot"
lrow = .Range("a" & .Rows.Count).End(xlUp).Row
lcol = .Cells(lrow, .Columns.Count).End(xlToLeft).Column
strWhere = .Range(.Cells(1, 1), .Cells(lrow, lcol)).Address
.PageSetup.PrintArea = strWhere
End With
End Sub
'---
Jim Cone
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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