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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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