Если вы создадите и загрузите диапазон в rng_to_add
, следующее будет принимать существующую PrintArea и Union
(добавить к) rng_to_add
:
' Going into this, you need to have declared a variable called rng_to_add
Dim rng_to_add As Range
' and loaded the range area you want to add to the PrintArea. This will
' be different for your particular situation.
Set rng_to_add = Sheets("Sheet1").Range("A1:C3")
' Referring to the current PageSetup of the Activesheet..
With ActiveSheet.PageSetup
' Check if the PrintArea of above PageSetup is empty
If .PrintArea = "" Then
' If so, set the PrintArea to the address of the Range: rng_to_add
.PrintArea = rng_to_add.Address
Else
' If not, set it to the address of a union (append) of the existing
' PrintArea's range and the address of the Range: rng_to_add
.PrintArea = Union(Range(.PrintArea), rng_to_add).Address
End If
' End the reference to the current PageSetup of the Activesheet
End With
Итак, дляПереносимость и / или интеграция в ваши существующие подпрограммы, вы можете создать подпрограммы, которые управляют PrintArea следующим образом:
Sub Clear_PrintArea()
' Set PrintArea to nothing
ActiveSheet.PageSetup.PrintArea = ""
End Sub
Sub Add_range_to_PrintArea(rng_to_add As Range)
' Referring to the current PageSetup of the Activesheet..
With ActiveSheet.PageSetup
' Check if the PrintArea of above PageSetup is empty
If .PrintArea = "" Then
' If so, set the PrintArea to the address of the Range: rng_to_add
.PrintArea = rng_to_add.Address
Else
' If not, set it to the address of a union (append) of the existing
' PrintArea's range and the address of the Range: rng_to_add
.PrintArea = Union(Range(.PrintArea), rng_to_add).Address
End If
' End the reference to the current PageSetup of the Activesheet
End With
End Sub
Затем вы можете вызвать это так:
Clear_PrintArea
Add_range_to_PrintArea Range("A1:C3")
Add_range_to_PrintArea Range("A7:C10")
Add_range_to_PrintArea Range("A13:C16")