【问题标题】:Object variable or With block variable not set issue对象变量或块变量未设置问题
【发布时间】:2023-04-04 14:02:01
【问题描述】:

我在执行多个 if 条件的循环时遇到问题。我正在做的是 1. 为“寻找什么和从哪里寻找”定义范围和单元格。 2.问题部分:我的If应该是三级的。 - 如果 x 为真,做事,否则 if2 - if2 为真,做事,否则 if3 - if3 为真,做事

循环下一个 x

由于某种原因,它运行了几次,但它给了我对象变量或未设置块变量。我该如何解决这个问题..?

错误在线:

If Not cl Is Nothing And Worksheets("Sheet1").Cells(x + 1, 7) = cl.Offset(0, -4) Then

 Sub Question()


Dim lr1 As Long
Dim lr2 As Long
Dim lr3 As Long
Dim lr4 As Long
Dim x As Long, y As Long, n As Integer
Dim arr As Variant, arr2 As Variant
Dim rng As Range, cl As Range
Dim rng2 As Range, c2 As Range


    n = 20 'Start row of Sheet1
    m = 20 'Start row of Sheet2
    o = 20 'Start row of Sheet3

    'Fill the array for a loop in memory
    With Blad6

        lr1 = Worksheets("Sheet4").Cells(.Rows.Count, 1).End(xlUp).Row
        arr = Worksheets("Sheet4").Range("A2:A" & lr1 + 1)
        lr3 = Worksheets("Sheet4").Cells(.Rows.Count, 1).End(xlUp).Row
        arr2 = Worksheets("Sheet4").Range("A2:A" & lr1 + 1)

    End With


    'Get the range to look in
    With Sheet1
        lr2 = Worksheets("Sheet5").Cells(.Rows.Count, 2).End(xlUp).Row
        Set rng = Worksheets("Sheet5").Range("H2:H" & lr2)
    End With


    With Blad6
    'Loop over the array and perform the search

    For x = 1 To UBound(arr)

       Set cl = rng.Find(arr(x, 1), LookIn:=xlValues)
        If Not cl Is Nothing And Worksheets("Sheet1").Cells(x + 1, 7) = cl.Offset(0, -4) Then
            'Things happen here

                n = n + 1

            Else



            If Not cl Is Nothing And Worksheets("").Cells(x + 1, 7) <> cl.Offset(0, -4) And cl.Offset(0, -4) <> 0 And cl.Offset(0, -5) > Worksheets("").Cells(x + 1, 3) Then
           'Things happen here
                m = m + 1

            Else

            If cl Is Nothing Then

              'Things happen here

                o = o + 1

        End If
        End If
        End If

    Next

End With

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic



End Sub

【问题讨论】:

  • 哦,对不起,添加到原版中,错误在线 If Not cl Is Nothing And Worksheets("Sheet1").Cells(x + 1, 7) = cl.Offset(0 , -4) 然后
  • 你的 with 语句没有任何意义,因为你没有使用它们。

标签: excel vba object


【解决方案1】:

VBA 评估AND 语句的两边,所以如果 cl 什么都不是,它仍然会尝试在第二部分使用它(给你一个错误)......

你需要像这样嵌套 2 个 If

If Not cl Is Nothing 
  If Worksheets("Sheet1").Cells(x + 1, 7) = cl.Offset(0, -4) Then
    ' do stuff here if cl is valid and offset condition is met
  Else
    ' do stuff if cl is valid but does not meet the offset condition 
  End If
Else
    ' do something when cl is nothing 
    ' could be the same thing as in above Else
    ' assuming cl is not involved in the operation
End If

如果您使用的是 VB.NET,那么您可以改用 AndAlso 并将其构建得更像您拥有的那样,但您没有,所以您不能。

【讨论】:

  • 好的,我把它改成了嵌套的 If。但是,我在这里遇到了一个相当新手的问题,因为我的 If 语句没有按照我的意愿移动。如果我的第一个“if-sentence 为假”,我希望它移至“else”,或者如果第二个句子为假,我希望它移至“else”。因此,如果其中一个为假,它应该移至相同else 函数。由于某种原因,我无法执行此操作...
  • 我为你更新了答案。你是这个意思吗?
猜你喜欢
  • 1970-01-01
  • 2018-12-11
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多