打印宏 - 运行时错误1004.方法'范围'对象' _Worksheet'失败

时间:2015-04-10 21:51:02

标签: excel vba

我想要做的是,如果A *列中有“1”,那么我希望宏将行复制,从列B *:L *复制到新工作表,然后打印工作表已经完成了整个范围。

如果A列中没有“1”,那么我只想让它继续下一行来检查。会不会有所帮助?

Sub PrintFlaggedRows()
  Const STARTSEARCHROW As Long = 1
  Const STARTPRINTROW As Long = 2
  Const ENDSEARCHROW As Long = 250
  Const STARTCOLUMN As Integer = 1 ' Column A
  Const ENDCOLUMN As Integer = 1   ' Column A
  Dim oldAlerts As Boolean
  Dim oldUpdates As Boolean
  Dim destSheet As Worksheet
  Dim srcSheet As Worksheet
  Dim destRange As Range
  Dim i As Long


  oldUpdates = Application.ScreenUpdating
  Application.ScreenUpdating = False
  oldAlerts = Application.DisplayAlerts
  Application.DisplayAlerts = False

  Set srcSheet = Sheets("Estimating & Building Quote")
  Set destSheet = Worksheets.Add
  Set destRange = destSheet.Cells(STARTPRINTROW, 1)

  For i = STARTSEARCHROW To ENDSEARCHROW

        If (srcSheet.Cells(i, 1) = 1) _
              Or (srcSheet.Cells(i, 1) = "1") Then

              srcSheet.Range(Cells(i, STARTCOLUMN), Cells(i, ENDCOLUMN)).Copy
              dstRange.PasteSpecial xlPasteValues
              dstRange.PasteSpecial xlPasteFormats

              Set dstRange = dstRange.Offset(0, 1)
        End If
  Next i

  destSheet.Columns.AutoFit
  destSheet.PrintOut

  destSheet.Delete

  Application.DisplayAlerts = oldAlerts
  Application.ScreenUpdating = oldUpdates

End Sub

1 个答案:

答案 0 :(得分:0)

srcSheet.Range(Cells(i, STARTCOLUMN), Cells(i, ENDCOLUMN)).Copy

此处,不合格的Cells()始终指向ActiveSheet,这可能不是您想要的。

尝试:

With srcSheet
    .Range(.Cells(i, STARTCOLUMN), .Cells(i, ENDCOLUMN)).Copy
End With