选择时出现过去的VBA错误

时间:2014-02-11 15:51:37

标签: excel vba excel-vba

我想知道是否有人可以帮助我。我有一个宏,它选择名为employees的工作表,并根据工作地点将它们放入正确的工作簿中。

我制作了宏,以便选择软件仓库的所有工作表,然后将它们复制到新的工作簿中。

我的问题是,当它无法找到其中一个工作表时,它会跳过该位置工作簿的所有工作表。并移动到下一个位置。有没有办法解决这个问题,所以如果宏无法找到其中一张纸,那么无论如何它都会移动它们。

Sub BIR()
    On Error GoTo Getout
    Sheets(Array("Martyn Arthur Lewis", "Norman Stewart Gray")).Move
    Sheets.Select
    For Each ws In Worksheets
       ws.Activate
       With ActiveSheet.PageSetup
          .PrintHeadings = False
          .PrintGridlines = False
          .PrintComments = xlPrintNoComments
          .Orientation = xlLandscape
          .Draft = False
          .PaperSize = xlPaperA4
          .FirstPageNumber = xlAutomatic
          .Order = xlDownThenOver
          .BlackAndWhite = False
          .Zoom = 90
          .printerrors = xlPrintErrorsBlank
          .OddAndEvenPagesHeaderFooter = False
          .DifferentFirstPageHeaderFooter = False
          .ScaleWithDocHeaderFooter = True
          .AlignMarginsHeaderFooter = False
       End With
    Next
    ChDir "\\afi-uplift\documents\company\Support Services\Support Services Level 1\Reports\Transport Reports\Vehicle KPI"
    ActiveWorkbook.SaveAs Filename:="\\afi-uplift\documents\company\Support Services\Support Services Level 2\Support Services\Transport\Drivers\Driver Performance\BIR Driver KPI " & Format(Date, "yyyy.mm.dd") & ".xlsx" _
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
           Windows("Driver Report.xlsm").Activate
  Getout:
End Sub

1 个答案:

答案 0 :(得分:0)

我不明白为什么人们总是需要使用.select和.activate 首先,它会减慢程序的速度,其次,通常,您甚至不需要选择/激活。

如果您这样编写代码,那么代码是否有效:

option explicit 'forces user to dim variables, , alot easier to find errors

err.clear
on error goto 0 'how can you debug errors with a on error goto (or on error resume next) ?

dim ws as worksheet    

For Each ws In Sheets(Array("Martyn Arthur Lewis", "Norman Stewart Gray"))
   With ws.PageSetup
       'your code
   end with
next ws