2014-04-07 122 views
0

嗨我想在多張紙上運行這個宏。任何幫助?跨越多張紙的宏

Sub Macro() 


     With ActiveSheet.PageSetup 
     .Zoom = 50 
    .Orientation = xlPortrait 
       .FitToPagesWide = 1 
       .FitToPagesTall = 1000 
     For Each cell In Range("D:D") 
    If InStr(cell, "We warrant") > 0 Then ActiveSheet.HPageBreaks.Add (ActiveSheet.Rows(cel.Row + 1)) 
      Next 
      End With 
     End Sub 

回答

1

下面的代碼遍歷每個工作表。

Sub Macro() 
    Dim sht As Worksheet 
    For Each sht In ThisWorkbook.Sheets 

     With sht.PageSetup 
      .Zoom = 50 
      .Orientation = xlPortrait 
      .FitToPagesWide = 1 
      .FitToPagesTall = 1000 
      For Each cell In Range("D:D") 
       If InStr(cell, "We warrant") > 0 Then sht.HPageBreaks.Add (sht.Rows(cel.Row + 1)) 
      Next 
     End With 
    Next 
End Sub 

此外,而不是通過所有細胞column D循環您可以在下面使用有更好的表現。

last_row = sht.Range("D" & Rows.Count).End(xlUp).Row 
      Set rng = sht.Range("D1:D" & last_row) 

      For Each cell In rng 
       If InStr(cell, "We warrant") > 0 Then sht.HPageBreaks.Add (sht.Rows(cel.Row + 1)) 
      Next