VBA - 错误的原因

时间:2017-06-14 20:13:00

标签: excel vba

我得到一个'对象变量或者没有设置块方差'错误,原因我无法弄清楚。使用错误处理程序后,VBA代码似乎按预期工作。我想解决错误,而不是跳过它。 任何人都可以指出这个错误发生在哪里/为什么?或者对此代码的任何其他建议?任何帮助表示赞赏!

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Tx As Range
Dim Rw As Variant

On Error GoTo E_H 'If this error handler is not in effect, and an edit is
                  '    made in column D, the error occurs.

Set Tx = Range("E44")
Set Rw = Rows("47")

If Application.Intersect(Tx, Range(Target.Address)).Value = "x" Then
    Application.EnableEvents = False
        With Range("C45")
            .Value = "T - 10"
        End With
        With Range("C45").Characters(Start:=36, Length:=5).Font
            .Color = -16776961
        End With
        With Range("I45")
            .Value = "T - 10 - LOS"
        End With
    Rw.Hidden = False
        With Range("B48")
            .Formula = "=B47+1"
        End With
    Sheets("DropDowns").Range("M6").Value = "65"
    Application.EnableEvents = True

Else

    Application.EnableEvents = False
        With Range("C45")
        .Value = "25Ac"
        End With
            With Range("I45")
            .Value = "25Ac - LOS"
        End With
    Rw.Hidden = True
        With Range("B48")
            .Formula = "=B46+1"
        End With
    Sheets("DropDowns").Range("M6").Value = "64"        
    Application.EnableEvents = True
End If

E_H: 
    Application.EnableEvents = True
    Exit Sub

End Sub

2 个答案:

答案 0 :(得分:0)

当Target不包含E44:

时,此行将引发错误
Intersect

Nothing将返回NothingValue没有 If Not Application.Intersect(Tx, Range(Target.Address)) Is Nothing And _ Tx.Value = "x" Then 属性

工作版可能是:

$userInputMin = 600;
$userInputMax = 2300;

function findCarWithinRange($lower, $upper) {
  $lowerLimit = 0;
  $upperLimit = 999999;
  $lower = max($lower, $lowerLimit);
  $upper = min($upper, $upperLimit);
  $cars = ['a' => 500, 'b' => 1000, 'c' => 1500, 'd' => 2000, 'f' => 2500];

  $carsWithinRange = array();

  foreach ($cars as $key => $value) {
      if ($value >= $lower && $value <= $upper) {
          $carsWithinRange[$key] = $value;
      }
  }

  return $carsWithinRange;
}

print_r(findCarWithinRange($userInputMin, $userInputMax));
// output: Array ( [b] => 1000 [c] => 1500 [d] => 2000 ) 

答案 1 :(得分:0)

问题似乎就在这一行。

If Application.Intersect(Tx, Range(Target.Address)).Value = "x" Then
'better and more succinct as,
If Application.Intersect(Tx, Target).Value = "x" Then

你不知道Target是否与Range("E44")相交,但是你试图从交叉点获取值,而不确定是否有任何东西可以从中获取值。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Range("E44"), Target) Is Nothing Then
        On Error GoTo E_H
        Application.EnableEvents = False

        If LCase(Range("E44").Value) = "x" Then
            With Range("C45")
                .Value = "T - 10"
                .Characters(Start:=36, Length:=5).Font.Color = -16776961
            End With
            Range("I45") = "T - 10 - LOS"
            Range("B48").Formula = "=B47+1"
            Rows("47").EntireRow.Hidden = False
            Worksheets("DropDowns").Range("M6").Value = 65
        Else
            Range("C45") = "I45"
            Range("I45") = "25Ac - LOS"
            Range("B48").Formula = "=B46+1"
            Rows("47").EntireRow.Hidden = True
            Worksheets("DropDowns").Range("M6").Value = 64
        End If
    End If

E_H:
    Application.EnableEvents = True

End Sub

我不确定我的逻辑流程是否正确,但这是我最好的猜测。如果E44发生变化并且现在为x,请取消隐藏第47行,调整公式等。如果E44发生变化且不是x,则隐藏第47行,调整公式等。