VBA运行时错误91

时间:2016-02-23 04:44:43

标签: vba excel-vba excel

我正在尝试进行一些计算以通过将值与其行中的平均值进行比较来验证单元格中的数据,并将验证结果放在另一个表中。

当我运行我的代码时,我得到了:

  

运行时错误'91':对象变量或未设置块

我不知道我错过了什么。所以,这是我的代码:

Private Sub CommandButton17_Click()
Dim SelectedCheckCell As Range, LastValue As Range, ValidationValue As Range, LastValidatedCell As Long
Dim Average As Integer, SelectedRow As Integer, LastValidation As Integer
Dim result As String, result2 As String, SelectedCol As String

Set SelectedCheckCell = Application.InputBox("Select initial cell to validate:", xTitleId, Selection.Address, Type:=8)
Set LastValue = Application.InputBox("Select last cell to calculate:", xTitleId, Selection.Address, Type:=8)
Set ValidationValue = Application.InputBox("Select initial cell to place result of validate:", xTitleId, Selection.Address, Type:=8)

SelectedRow = Split(SelectedCheckCell.Address, "$")(2)
SelectedCol = Split(LastValue.Address, "$")(1)

LastValidatedCell = Cells(Rows.Count, 2).End(xlUp).Row
LastValidation = Split(LastValidatedCell.Address, "$")(2)

result = "OK"
result2 = "NOT OK"

For i = SelectedRow To LastValidation

    Average = Application.WorksheetFunction.Average(Range("C" & i & ":" & SelectedCol & i))

    If ((SelectedCheckCell.value < (Average - (Average * 0.1))) Or (SelectedCheckCell.value > (Average + (Average * 0.1)))) Then 
        ValidationValue.value = result
    Else
        ValidationValue.value = result2

End If

SelectedCheckCell = SelectedCheckCell.Offset(1, 0)
ValidationValue = ValidationValue.Offset(1, 0)

Next i

End Sub

谢谢!

1 个答案:

答案 0 :(得分:-1)

更改此

SelectedCheckCell.Address

Selection.Address

不确定你要对这些变量做什么(LastValue.Address&amp; ValidationValue.Address),因为它们在调用时不会组成和调整对象而不在其上设置任何对象。