VBA to assign print area dynamically

cubefarmed

New Member
Joined
Dec 8, 2011
Messages
6
Greetings coders! I have a problem. My workbook contains two sheets appropriately named Data Dump and Formatting.

Within the Data Dump, I want to establish the amount of rows by using CountA("A3:A50"). For the sake of this question lets say there are 5 data rows.

I want to set my print area on Formatting to be B1 to H165. Columns B and C must be part of the print area, but the column disposition begins with the D column.

Example: If I have 10 rows in Data Dump, I want the print area to be B1:M165. Each additional row increases the Column of my print area by 1.

I hope this makes sense. I tried to conceptualize this in terms of variables and declarations I just started confusing myself. I'm a poor poor VBA student :(

Thanks for any help!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
lastColmNo = Application.WorksheetFunction.CountA(Sheets("DataDump").Range("A3:A50")) + 3
Sheets("Formatting").PageSetup.PrintArea = Range(Cells(1, 2), Cells(165, lastColmNo)).Address
?
 
Upvote 0
<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><br>    i = Application.WorksheetFunction.CountA(Sheets("Data Dump").Range("A3:A50"))<br>    <br>    <SPAN style="color:#00007F">With</SPAN> Sheets("Formatting")<br>        .PageSetup.PrintArea = .Range("B1:M165").Resize(, 2 + i).Address<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>
 
Upvote 0
You both are amazing! Thank you so much!!

I used both of them and they both work. VBA is amazing. I need to work harder at learning it.

Truly appreciate the help. Have a great weekend.
 
Upvote 0

Forum statistics

Threads
1,216,747
Messages
6,132,483
Members
449,729
Latest member
davelevnt

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