【问题标题】:Selecting Multiple Dynamic Ranges at Once一次选择多个动态范围
【发布时间】:2020-08-13 19:29:24
【问题描述】:

我有这张工作表,我想在其中打印两页,我尝试进行分页,但由于某种原因,即使我自己手动进行也无法正常工作。所以,正因为如此,我试图通过一次选择多个范围来选择我想要打印的页面(见下图)。问题是每次我运行宏时范围都会改变。我无法找到组合这个范围的方法。我的意图是将数字连接起来创建一个变量,例如:Range("A1: T & lastrow") 以便“选择”所有需要的范围。

代码如下所示:


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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    试试这个

    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
    

    【讨论】:

    • 我应该说第二页要打印在纸张的背面。我在禁用 Backside 功能的情况下尝试了您的代码,并且它有效。一旦我启用它,它只会打印一页。我不指定那个细节是不好的。
    • 已更新。但是,请注意,这仅在两个范围之间存在差距时才有效(即 lrow1 - lrow > 1)。否则,excel会将两个范围合二为一
    猜你喜欢
    • 2014-03-04
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多