在With块

时间:2019-05-15 00:09:13

标签: excel vba

这是Excel中的vba脚本。尝试在With块内的另一个工作表中查找参考值时遇到错误

我对Excel中的vba脚本不熟悉。我从该站点阅读了一些代码片段,并对其进行了尝试。不知道为什么我会不断收到此错误

  

运行时错误“ 91”。

我发现这表示“对象变量或未设置块变量”

Public Sub ReplaceCovCfg()

    Dim sValToFind As String
    Dim rSearchRange As Range
    Dim sFirstAdd As String
    Dim rFoundCell As Range
    Dim rAllFoundCells As Range
    Dim sSbmId As String
    sValToFind = "Reference"
    With ThisWorkbook.Worksheets("Internal_Report")
        Set rSearchRange = .Range(.Cells(1, 2), .Cells(.Rows.Count, 2).End(xlUp))
    End With

    With rSearchRange
         Set rFoundCell = .Find(sValToFind, LookIn:=xlValues, LookAt:=xlPart)
         If Not rFoundCell Is Nothing Then
            sFirstAdd = rFoundCell.Address
            Do

                sMessage = sMessage & rFoundCell.Row & ", "

                'Create a range of found cells.
                If Not rAllFoundCells Is Nothing Then
                    Set rAllFoundCells = Union(rAllFoundCells, rFoundCell)
                Else
                    Set rAllFoundCells = rFoundCell
                End If
                Set rFoundCell = .FindNext(rFoundCell)

                ' search in previous records
                sSbmId = rFoundCell.Offset(0, 1).Value
                With ThisWorkbook.Worksheets("Sheet3")
                    Set lookupSearchRange = .Range(.Cells(1, 2), .Cells(.Rows.Count, 2).End(xlUp))
                    With lookupSearchRange
                        Set lookupFoundCell = .Find(sSbmId, LookIn:=xlValues, LookAt:=xlPart)
                    ' End With

                        If Not lookupFoundCell Is Nothing Then
                            rFoundCell.Offset(0, 4).Value = "Old"
                        Else
                            rFoundCell.Offset(0, 4).Value = "New"
                        End If
                    End With
                End With

            Loop While rFoundCell.Address <> sFirstAdd
         End If
    End With

    rAllFoundCells.Offset(0, 1).Copy Destination:=ThisWorkbook.Worksheets("Sheet2").Range("A2")

End Sub

继续收到此错误:

  

运行时错误'91':对象变量或未设置块变量

此行:

Set lookupFoundCell = .Find(sSbmId, LookIn:=xlValues, LookAt:=xlPart)

如果我删除此行以及下面的If块,脚本将成功运行而不会出错。

如果我用一个常量字符串(例如“ blahblah”)替换sSbmId,我仍然会遇到相同的错误。

出于好奇的缘故,如果我将sSbmId替换为sValToFind(这没有意义,则宏运行时不会出错。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

set分配需要一个对象,但尚未声明(尺寸较大)lookupFoundCell。因此,默认将其设置为变体。

Range.Find方法返回一个Range对象,因此您可以像这样对变量进行尺寸标注:

Dim lookupFoundCell as Range 

始终在您的VBA项目中使用Option Explicit,这样您将获得更好的错误检查。在这种情况下,您会看到没有声明变量。

答案 1 :(得分:0)

使用 F8 逐行浏览,您将发现问题实际上在这里:

If Not rAllFoundCells Is Nothing Then
    Set rAllFoundCells = Union(rAllFoundCells, rFoundCell) 'This Line Errors
Else
    Set rAllFoundCells = rFoundCell
End If

原因很简单:您的 first rFoundCell来自ThisWorkbook.Worksheets("Internal_Report"),所以rAllFoundCells也位于ThisWorkbook.Worksheets("Internal_Report")

但是,您的 second rFoundCell来自ThisWorkbook.Worksheets("Sheet3")-您正试图从不同的工作表中创建Union个单元格。这是不允许的操作。

要更清楚地了解这一点(但不修复代码-您需要先确定自己实际上是在做什么,然后再对底层逻辑进行重新设计),请使用:

If Not rAllFoundCells Is Nothing Then
    Debug.Print "rAllFoundCells: " & rAllFoundCells.Worksheet.Name
    Debug.Print "rFoundCell:" & rFoundCell.Worksheet.Name
    Set rAllFoundCells = Union(rAllFoundCells, rFoundCell) 'This Line Errors
    Debug.Print "This line will only show if there is no error"
Else
    Set rAllFoundCells = rFoundCell
End If

您可以在VBE立即窗口( Ctrl + G )中查看调试输出