"运行时错误91"设置变量

时间:2015-11-02 23:46:26

标签: excel vba excel-vba

我遇到运行过去曾运行过的VBA脚本的问题。代码尝试从数据库和已生成的Excel工作表中获取数据,然后运行更多计算以创建可用的统计信息。这部分是错误的来源:

Dim strCat, strPlan, strCustodian As String
Dim i, lAssets As Long
Dim cn As ADODB.Connection, rs As ADODB.Recordset 'ms access connection
Public Const cnnstr As String = "Provider=Microsoft.ACE.OLEDB.12.0; " & _
        "Data Source=G:\OADBO.accdb;"
Dim rngA, rngB As Range
Option Explicit

Private Sub MapExisting()

Set rngA = wsEntry.Range("A1")



For i = 1 To WorksheetFunction.CountIf(wsEntry.Columns(1), "*") - 1
Set rngB = wsMapping.Range("B1")


Set rngA = wsEntry.Columns(1).Find(What:="*", After:=rngA, _
    LookIn:=xlValues, LookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False)


    strCat = rngA.Offset(0, 2).Value
    lAssets = rngA.Offset(0, 1).Value


Call UnprotectSheets


    Set rngB = wsMapping.Columns(2).Find(What:=strCat, After:=rngB, _
        LookIn:=xlFormulas, LookAt:=xlPart, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False)


        rngB.End(xlUp).Offset(1, 0).Value = rngA    * <——ERROR IS HERE *
        rngB.End(xlUp).Offset(0, 3).Value = lAssets

Jump:
Next i

'finds all empty rows and keeps as range- then hides all in range
'Call Hiderows

Call ProtectSheets

Application.ScreenUpdating = True

wsMapping.Activate



End Sub 

1 个答案:

答案 0 :(得分:1)

如果第一个查找操作失败,rngA将设置为Nothing。

如果发生这种情况,您在分配时会出错(显示ERROR的行在此处)。

您可以这样检查:

If rngA Is Nothing Then