I'm making a macro that sets a print area to user selected areas of document. Basically there is a box next to a bunch of cells and if user ticks the box then the bunch of cells is included to the print area. Here is my code so far:
Sub TestCellA1()
Dim t As Integer, d As Integer
t = 0
d = 20
Dim rng_per As Range
Set rng_per = Range("A3:E328") 'prints whole document
Dim rng1 As Range
If Not IsEmpty(Range("F19")) = True Then
ActiveSheet.PageSetup.PrintArea = Range(rng_per)
Else
Do While t < 10
If IsEmpty(Range("F" & d).Value) = True Then
'MsgBox "Do not print"
Else
'MsgBox "Do print"
ActiveSheet.PageSetup.PrintArea = rng1
End If
t = t + 1
d = d + 25
Loop
End If
End Sub
So far this works to the point where the actual work is supposed to be done. I planned that everytime when loop finds box ticked it adds that part of document to the print area. As a newbie with vba I have no idea how to add those areas to print area. Any ideas how to do it? Thanks in advance& have a good day.
If you create and load a range into
rng_to_add
, the following will take the existing PrintArea andUnion
(append to) therng_to_add
:So, for portability and/or integrating into your existing routines, you could create subroutines that manage the PrintArea like so:
You could then call it like so:
You could do this a few different ways I assume, but here is my suggestion:
You will assign a cell to the checkboxes. Assign a formula that if value is true (if box is checked) then create example range "A1:B6" (Change accordingly).
In your macro code loop through the range of cells that either are empty or contain a range (in my suggestion you could use a loop):
Assign this macro to all your checkboxes and tinker around with it. It should work for you (couldn't test it)