vba - Excel vba 对象变量或未设置 block 变量错误

标签 vba variables excel

我在 excel vba 中编码,运行代码时出现错误 91 或 Object 变量或 With block variable not set 错误,我不确定为什么。我定义并设置了我的变量,所以我不知道是什么导致了错误。相关代码如下

  Sub Button5_Click()
Dim i As Integer
Dim Month As Range
Dim Avg As Range
Dim Target As Range
Dim Incorrect As Range

Set Month = Range("J19")
Set Avg = Range("H19")
Set Incorrect = Range("A19")
Set Target = Range("M19")

'Range("A" & Rows.Count).End(xlUp).Row
For i = 0 To 1000
If IsEmpty(Avg) Then

If Month.Find("jan") <> "" Then
Set Target = Range("M19")

错误出现在 If Month.Find("jan") <> ""then 部分代码上。

完整代码在这里:
Sub Button5_Click()
Dim i As Integer
Dim Month As Range
Dim Avg As Range
Dim Target As Range
Dim Incorrect As Range

Set Month = Range("J19")
Set Avg = Range("H19")
Set Incorrect = Range("A19")
Set Target = Range("M19")

'Range("A" & Rows.Count).End(xlUp).Row
For i = 0 To 1000
If IsEmpty(Avg) Then

If Month.Find("jan") <> "" Then
Set Target = Range("M19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("feb") <> "" Then
Set Target = Range("O19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("mar") <> "" Then
Set Target = Range("Q19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("apr") <> "" Then
Set Target = Range("S19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("may") <> "" Then
Set Target = Range("U19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("jun") <> "" Then
Set Target = Range("W19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("jul") <> "" Then
Set Target = Range("Y19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("aug") <> "" Then
Set Target = Range("AA19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("sep") <> "" Then
Set Target = Range("AC19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("oct") <> "" Then
Set Target = Range("AE19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("nov") <> "" Then
Set Target = Range("AG19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

Else
Set Target = Range("AI19")

If IsEmpty(Target) Then
Incorrect.Value = "X"

End If
End If

Else
If Month.Find("jan") <> "" Then
Set Target = Range("N19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("feb") <> "" Then
Set Target = Range("P19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("mar") <> "" Then
Set Target = Range("R19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("apr") <> "" Then
Set Target = Range("T19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("may") <> "" Then
Set Target = Range("V19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("jun") <> "" Then
Set Target = Range("X19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("jul") <> "" Then
Set Target = Range("Z19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("aug") <> "" Then
Set Target = Range("AB19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("sep") <> "" Then
Set Target = Range("AD19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("oct") <> "" Then
Set Target = Range("AF19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

ElseIf Month.Find("nov") <> "" Then
Set Target = Range("AH19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If

Else
Set Target = Range("AJ19")

If IsEmpty(Target) Then
Incorrect.Value = "X"
End If
End If
End If
Set Month = Month.Offset(1, 0)
Set Incorrect = Incorrect.Offset(1, 0)
Set Avg = Avg.Offset(1, 0)
Set Target = Target.Offset(1, 0)

Next i
End Sub

任何帮助是极大的赞赏。

最佳答案

Range.Find返回 Range所以你需要将它作为一个对象来评估:

If Not Month.Find("jan") Is Nothing Then
    Set Target = Range("M19")
End If

如果您需要使用返回的Range你可以使用这样的东西:
Dim foundCell As Range
Set foundCell = Month.Find("jan")

If Not foundCell Is Nothing Then
    Set Target = Range("M19")
End If
Range.Find 的引用资料是 here .

关于vba - Excel vba 对象变量或未设置 block 变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27991222/

相关文章:

在C中使用超出其范围的变量进行计算

variables - 如何将变量的值用作变量本身?

Excel 2007 重复检查错误地将十六进制转换为指数

arrays - VBA 为什么Application.Countif返回数组或错误424

c# - Excel文件的VBA代码中的文本替换

excel - VBA和Excel优化处理时间,处理多行

ruby-on-rails - Rails- Ruby 实例变量

excel - Angular 6 下载 API excel 文件

excel - VBA:可以在运行时配置一组 "constants"吗?

excel - 将单元格内部颜色从单击的单元格复制到事件单元格