Macro to set print area

bmt216a

New Member
Joined
Dec 15, 2022
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone, I want to create a macro that will set the print area when the rows filled with data are always changing. Eg. One day data may fill 20 rows, another day may fill 90 rows. I need to set print area from columns A-D down to an undetermined row that is always changing based on how much data I have that day. The first row to print will always be 1 and the last row will be the one with data. Thank you for you help.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Use this:

VBA Code:
Sub SetPrintArea()
  Dim lr As Long
  lr = Range("A:D").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  ActiveSheet.PageSetup.PrintArea = "$A$1:$D$" & lr
End Sub
 
Upvote 0
Use this:

VBA Code:
Sub SetPrintArea()
  Dim lr As Long
  lr = Range("A:D").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  ActiveSheet.PageSetup.PrintArea = "$A$1:$D$" & lr
End Sub
Thank you, I've just tried it and it doesn't seem to be doing what I want. I have attached a screenshot of the print preview here to show you what I mean. I don't want any additional rows printed after the "CC" row as that is where the data stops, but it is still showing those rows to be printed. I want the macro to dynamically adjust the print area based on where the last row with data is located and not print anything else after.
 

Attachments

  • Screenshot (154).png
    Screenshot (154).png
    64.3 KB · Views: 12
Upvote 0
I've just tried it and it doesn't seem to be doing what I want.

lr = Range("A:D").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
That line tests for the last cell with data in columns A, B, C, and D. That was your original request:
I need to set print area from columns A-D

In your image the columns are not visible, but I assume that the first column is column A, so you must adjust the line that evaluates the last cell with data to this:

VBA Code:
Sub SetPrintArea()
  Dim lr As Long
  lr = Range("B:C").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  ActiveSheet.PageSetup.PrintArea = "$A$1:$D$" & lr
End Sub

I am only evaluating columns B and C, because in your image I can see that in column D you have zeros, those cells are not empty.
 
Upvote 0
Solution
That line tests for the last cell with data in columns A, B, C, and D. That was your original request:


In your image the columns are not visible, but I assume that the first column is column A, so you must adjust the line that evaluates the last cell with data to this:

VBA Code:
Sub SetPrintArea()
  Dim lr As Long
  lr = Range("B:C").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  ActiveSheet.PageSetup.PrintArea = "$A$1:$D$" & lr
End Sub

I am only evaluating columns B and C, because in your image I can see that in column D you have zeros, those cells are not empty.
I should have been more clear. You are correct columns B and C are being evaluated. As for column D, there is a simple formula in every other cell that calculates the difference between two values in column B. Your code did the trick. Thank you so much for your help. If you don't mind, could you explain a little about how you got to this solution? Thank you
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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