为什么会出现此错误Object' _workbook#39;我的宏发生失败?

时间:2012-09-13 07:28:30

标签: vba excel-vba excel

我开始为excel 2007学习VBA。任何人都可以帮助执行此错误的原因:

代码是

Public Sub LastVariable_Check()
    'Check if Last Variable is of latest month sales

    'lastvariable  = lv

    lv = wkbOut.Sheets("Sheet1").Range("B1",Range("B1").End(xlToRight)).End(xlToRight).Text
    If "0" & Mid(lv, InStr(lv, "Month/") + 6, 1) = strmth _
    And Mid(lv, InStr(lv, "Month/") + 8, 4) = strYear Then
        Set wkbRaw = Workbooks.Open(strInputQCPath & "Errorlog.xlsx")

        wkbRaw.Sheets("Sheet1").Range("A1").Offset(i, 2).Value = "Correct"
        wkbRaw.SaveAs Filename:=strInputQCPath & "Errorlog.xlsx"
        wkbRaw.Close
    Else
        Set wkbRaw = Workbooks.Open(strInputQCPath & "Errorlog.xlsx")

        wkbRaw.Sheets("Sheet1").Range("A1").Offset(i, 2).Value = "Incorrect"
        wkbRaw.SaveAs Filename:=strInputQCPath & "Errorlog.xlsx"
        wkbRaw.Close
    End If 
End Sub

请帮助
谢谢!

1 个答案:

答案 0 :(得分:0)

我已经调用了这个LastVariable_Check,超出了本工作簿wkbOut的范围。只是剪切粘贴并在代码之前调用它,我将wkbOut保存为不同的工作表名称并且它有效。