Vba 单元格。查找错误91

Vba 单元格。查找错误91,vba,Vba,我对这个代码有问题。我试图找出这段代码的错误,但找不到解决方案 Sub RDB_Worksheet_To_PDF() Dim FileName As String Dim PONumber As String Dim FolderPath As String PONumber = Sheets("Purchase Order with Sales Tax").Cells(8, 6).Value If ActiveWindow.Sel

我对这个代码有问题。我试图找出这段代码的错误,但找不到解决方案

 Sub RDB_Worksheet_To_PDF()
        Dim FileName As String
        Dim PONumber As String
        Dim FolderPath As String

 PONumber = Sheets("Purchase Order with Sales Tax").Cells(8, 6).Value

    If ActiveWindow.SelectedSheets.Count > 1 Then
        MsgBox "There is more then one sheet selected," & vbNewLine & _
               "be aware that every selected sheet will be published"
    End If
'Call the function with the correct arguments
    FileName = RDB_Create_PDF(ActiveSheet, FolderPath & PONumber, True, True)

    If FileName <> FolderPath & PONumber Then
        'Ok, you find the PDF where you saved it
        'You can call the mail macro here if you want
        MsgBox "Sweet! The PO has been saved as a PDF." & vbNewLine & _
               "Click on the PO Number in the PO Number WorkSheet to view."
    Else
        MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
               "Microsoft Add-in is not installed" & vbNewLine & _
               "There is no PO number selected" & vbNewLine & _
               "The path to Save the file in is not correct" & vbNewLine & _
               "You didn't want to overwrite the existing PDF if it exist"
    End If

    Sheets("PO Number").Select
    Range("A1").Select
是因为我没有使用.activate还是我没有使用set=? 请让我知道我需要做什么


干杯

基本上,您正在尝试查找单元格并同时激活它,如果未找到任何内容,则会提示您所提到的错误。您可以先尝试查找,然后激活:

set smvar = Cells.Find(What:=PONumber, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False)  
if not smvar is Nothing then smvar.activate

代码的其余部分在哪里?信息似乎不完整。感谢您的评论,但我在本部分中收到另一条错误消息9 PONumber=Sheets(“含销售税的采购订单”)。单元格(8,6)。值是相同的单元格。查找错误91,还是运行时错误“9”?如果是错误9,请检查工作表的名称(空格/etc)。我还假设代码位于一个“模块”中。另外,请提及我看到的所有错误。在我将代码更改为您建议的设置smavar=之后,我在工作表(“含销售税的采购订单”)中发现了错误9。单元格(8,6)。值哦,您的意思是,在第一个错误修复后,会出现另一个错误。你试过检查你的工作表名称吗?你好,亚历克斯,谢谢你的帮助,我的代码现在运行良好。我没有看到任何错误消息。我不知道为什么我之前犯了错误9。
set smvar = Cells.Find(What:=PONumber, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False)  
if not smvar is Nothing then smvar.activate