简体   繁体   中英

Selecting Multiple Dynamic Ranges at Once

I have this worksheet where I want to print in two pages, I tried to do page breakups but for some reason is not working even doing it manually myself. So, because of that I'm trying to select the pages that I want to print by selecting multiple ranges at once (See figure below). The issue is that the range will change everytime I run the macro. I can not be able to find the way of putting together this range. My intend is to concatenate numbers an variables to create one, like for example: Range("A1: T & lastrow") in order to "select" all the range needed.

一次选择多个范围

The code looks like this:


Dim ws As Worksheet
Dim imprimir As Object
Dim lrow As Variant
Dim lrow1 As Variant
Dim lrow2 As Variant
Dim rn As Range
Dim LPics As Shape


For Each LPics In ActiveSheet.Shapes
lrow = Application.WorksheetFunction.Max(LPics.BottomRightCell.Row, lrow)
Next LPics
lrow = lrow + 1
lrow1 = lrow + 1
lrow2 = lrow1 + 40

'Here I tried to do the selecting but gives me method range of object _global failed
Set rn = Worksheets("Resultados").Range("A1:T & lrow, A & lrow1: T & lrow2") 


Set ws = ActiveWorkbook.Worksheets("Resultados")

With ws.PageSetup
.Zoom = False
.PaperSize = xlPaperLetter
.Orientation = xlLandscape
.HeaderMargin = 0#
.FooterMargin = 0#
.BottomMargin = 0#
.LeftMargin = 0#
.RightMargin = 0#
.TopMargin = 0#
.FitToPagesTall = 1
.FitToPagesWide = 1
End With

ws.PrintOut
End Sub

Try this

Sub Imprmir()

Dim ws As Worksheet
Dim imprimir As Object
Dim lrow As Variant
Dim lrow1 As Variant
Dim lrow2 As Variant
Dim rn As Range
Dim LPics As Shape


For Each LPics In ActiveSheet.Shapes
lrow = Application.WorksheetFunction.Max(LPics.BottomRightCell.Row, lrow)
Next LPics
lrow = lrow + 1
lrow1 = lrow + 1
lrow2 = lrow1 + 40

Set ws = ActiveWorkbook.Worksheets("Resultados")


'* You don't need this
'Here I tried to do the selecting but gives me method range of object _global failed
'Set rn = ws.Range("A1:T & lrow, A & lrow1: T & lrow2")



With ws.PageSetup
.Zoom = False
.PaperSize = xlPaperLetter
.Orientation = xlLandscape
.HeaderMargin = 0#
.FooterMargin = 0#
.BottomMargin = 0#
.LeftMargin = 0#
.RightMargin = 0#
.TopMargin = 0#
.FitToPagesTall = 1
.FitToPagesWide = 1

'Set print areas and print out
.PrintArea = "$A$1:$T$" & lrow & ",$A$" & lrow1 & ":$T$" & lrow2
ws.PrintOut

'Clear the print area
.PrintArea = ""
End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM