Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择时通过VBA错误_Vba_Excel - Fatal编程技术网

选择时通过VBA错误

选择时通过VBA错误,vba,excel,Vba,Excel,我想知道是否有人能帮助我。我有一个宏,可以选择命名为employees的工作表,并根据工作地点将其放入正确的工作簿中 我制作了宏,以便它选择仓库的所有工作表,然后将它们复制到新工作簿中 我的问题是,当它找不到其中一张工作表时,它会跳过该位置的所有工作表。并移动到下一个位置。有没有办法解决这个问题,如果宏找不到其中一个工作表,它会移动其余的工作表 Sub BIR() On Error GoTo Getout Sheets(Array("Martyn Arthur Lewis", "

我想知道是否有人能帮助我。我有一个宏,可以选择命名为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

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

如果您这样编写代码,代码是否正常工作:

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

请发布一些代码:)这是一个有点不清楚你想要实现什么。您能否澄清-以及代码在何处没有按预期工作?您需要在数组中循环并测试每个工作表是否存在:如果存在,则选择它:您可以使用(例如)
sheets(“Name2”)向选择中添加更多工作表。选择False
-
False
表示要添加到已选择的工作表中