【发布时间】: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 语句没有任何意义,因为你没有使用它们。