【问题标题】:Object variable or With block variable not set error in vba对象变量或块变量未在 vba 中设置错误
【发布时间】:2017-02-08 15:09:33
【问题描述】:

我的以下代码运行良好。我不知道发生了什么变化,突然我在行中收到错误“对象变量或未设置块变量”

Range(Test(0)) = wf.CountIfs(.Rows(1).Find(Test(2), lookat:=xlWhole).EntireColumn, Test(3))

我的代码:

Sub WBR()

Dim Count1Criteria As Variant
Dim Count3Criteria As Variant
Dim Test As Variant
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction



Filter1InSummary = Array(Array("AH4", "Latency", "Pass/Fail", "Pass"), _
                         Array("AH5", "Latency", "Pass/Fail", "Fail"), _
                         Array("AH44", "TT", "Able to repro", "Not Tested"), _
                         Array("AH47", "TT", "Reason for Reasssignment/Resolving", "Duplicate TT"), _
                         Array("AH51", "TT", "Able to repro", "Yes"), _
                         Array("AH52", "TT", "Able to repro", "No"), _
                         Array("AH61", "Reactive", "Item Type", "Item"), _
                         Array("AH46", "TT", "Reason for Reasssignment/Resolving", "Hardware Unavailable"), _
                         Array("AH41", "TT", "Severity", "2"), _
                         Array("AH62", "Reactive", "Trigger Key Name", "*App Crashes*"), _
                         Array("AH63", "Reactive", "Trigger Key Name", "*Download*"), _
                         Array("AH49", "TT", "Reason for Reasssignment/Resolving", "Insufficient Information"), _
                         Array("AH15", "Latency", "Comments", "*Waived since unable to repro issue*"), _
                         Array("AH6", "Latency", "Comments", "*Waived since unable to repro issue*"), _
                         Array("AH16", "Latency", "Comments", "*Waived due to business reasons*"), _
                         Array("AH18", "Non-Mhowls", "Type of testing", "Full Testing"), _
                         Array("AH19", "Non-Mhowls", "Type of testing", "Upgrade Testing"), _
                         Array("AH21", "DRG", "Failure testing type", "Normal Testing"), _
                         Array("AH22", "DRG", "Failure testing type", "Deep Testing"))




 For Each Test In Filter1InSummary
    With Worksheets(Test(1))
        Range(Test(0)) = wf.CountIfs(.Rows(1).Find(Test(2), lookat:=xlWhole).EntireColumn, Test(3))
    End With
Next

【问题讨论】:

  • 大概是找不到值吧?您只指定了一个可能没有帮助的参数。
  • 一个参数?你能用外行术语解释吗?我需要提供哪些信息才能找出这里出了什么问题?
  • 单步执行您的代码以检查发生了什么。如果您认为存在某个值但没有找到它,则可能是由于 Find 的参数之一。查看 VBA 帮助或例如 msdn.microsoft.com/en-us/library/office/ff839746.aspx(顺便说一句,我认为您只需要 CountIf。)
  • @stackmark 关于如何通过多个Worksheets 实现多个CountIfs 的好主意,请参阅下面的答案

标签: vba excel


【解决方案1】:

您忘记使用Worksheet(Test(1)) 完全限定您的Range(Test(0))

换行:

Range(Test(0)) = wf.CountIfs(.Rows(1).Find(Test(2), lookat:=xlWhole).EntireColumn, Test(3))

到:

.Range(Test(0)).Value = wf.CountIfs(.Rows(1).Find(Test(2), lookat:=xlWhole).EntireColumn, Test(3))

注意:我已将Value 添加为良好的编码实践,没有必要

【讨论】:

  • 感谢@Shai Rado 的建议。但是即使在我添加了建议的行之后,我也会在相同的代码中得到相同的错误...如果指定的任何 Col/Worksheet 不存在,是否会发生此错误?
【解决方案2】:

我修复了错误。我正在测试的测试数据中不存在其中一列!

一旦我找出丢失的数据并添加它,问题就解决了!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多