使用查找方法时,运行时错误91,

时间:2019-01-21 11:56:46

标签: excel vba find runtime-error

我希望您能为我不断遇到的错误提供帮助-我试图通过删除该行,然后求助于数据并确保所有公式正确,来修复报表中偶尔发生的数据错误。

我运行了宏记录器,并用它来帮助构建下面的代码-当我从记录器中运行代码时,一切正常,但是现在我尝试将其个性化到我的工作簿中(即添加工作簿名称)得到

  

此行的运行时91错误

cell = data.Range("A1:A400").Find(What:="Date", lookat:=xlWhole)

围绕我所做的导致错误的任何帮助,以便将来避免它,将非常有用!

Sub RemoveLine()
    Dim del, dr As Workbook
    Set dr = ThisWorkbook
    Set del = Workbooks("Delivery Data.xls")

    Dim rep, inf, data As Worksheet
    Set rep = dr.Sheets("Report")
    Set inf = dr.Sheets("Info")
    Set data = dr.Sheets("Data")

    Dim r1, r2, r3, r4, row01 As Long

    Dim nu, na As String

    nu = inf.Range("C7").Text
    na = inf.Range("C4").Text

    cell = data.Range("A1:A400").Find(What:="Date", lookat:=xlWhole)           
    MsgBox cell

    If Not cell Is Nothing Then          
        row01 = cell.Row
        data.Range("A" & row01 & ":S" & row01).Delete Shift:=xlUp
        data.Sort.SortFields.Clear
        data.Sort.SortFields.Add Key:=Range("A2:A21"), _
          SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

        With data.Sort
            .SetRange Range("A2:S21")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With


        'FORMULAS                    
        Dim st1, st2, st3, st4, st5, st6 As String

        st1 = "=IF(Data!A2="""", """", Data!A2)"
        st2 = "=IF(Data!C2="""", """", Data!C2)"
        st3 = "=IF(Data!G2="""", """", Data!G2)"
        st4 = "=IF(F3="""", """", G3/$C$10)"
        st5 = "=IF(E27="""", """", Data!G2)"
        st6 = "=IF(C27="""", """", G27/Info!$C$10)"

        With inf
            .Range("E3:E100").Formula = st1
            .Range("F3:F100").Formula = st2
            .Range("G3:G100").Formula = st3
            .Range("H3:H100").Formula = st4
        End With

        With rep
            .Range("B27:C62").Formula = st1
            .Range("E27:E62").Formula = st2
            .Range("G27:G62").Formula = st5
            .Range("J27:J62").Formula = st6
        End With       
    End If            
End Sub

1 个答案:

答案 0 :(得分:0)

您的错误可能是由于未声明cell,并且您没有使用Set关键字造成的。根据{{​​3}},Find上的Range返回Range(或Nothing),因此您应该:

Dim cell As Range
Set cell = data.Range("A1:A400").Find(What:="Date", LookAt:=xlWhole)