Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vba - Cells.find error 91

I have problems with this code. I tried to figure it what is wrong with this code, but cannot find the solution

 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

I get the error message 91 in this part

**Cells.Find(What:=PONumber, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate**

Is it because I did not use .activate or I did not use set = ? Please let me know what I need to do.

Cheers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically you are trying to find the cell AND activate it at the same time, which will prompt you the error mentioned if nothing is found. You could try find it first then activate:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...