[英]Set print area in excel to a relative reference
我想創建一個循環,在工作簿上打印所選范圍。 我選擇了一個相對范圍,該范圍存儲為“ Arge”。
我想將打印區域設置為所選范圍。
Range("A1").Select
ActiveCell.Offset(1, 0).Range("A1:P37").Select
Dim Arge As Range
Set Arge = Selection 'PROBLEM
'do loop and other code
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = Arge 'PROBLEM with Arge
兩件事情
代碼 :
Sub Sample()
Dim ws As Worksheet
Dim Arge As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set Arge = ws.Range("A1:P37")
With ws.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
.PrintArea = Arge.Address
End With
End Sub
注意事項 :
如果確實需要使用Selection
對象,請確保它是有效范圍。 例如
'~~> Check if what the user selected is a valid range
If TypeName(Selection) <> "Range" Then
MsgBox "Select a range first."
Exit Sub
Else
Set Arge = Selection
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.