VBA脚本 - 编译错误:参数数量错误

时间:2015-08-20 05:34:11

标签: vba loops if-statement cell

我尝试创建一个基本的VBA脚本来检查一列单元格,并在找到特定值时将内容添加到不同列的同一行。

我在这上花了几个小时,最初它跑了但没有结果。现在我重新考虑了代码,无论我做什么,我都会遇到编译错误:

  

参数数量错误或属性分配无效。

这是我的代码 -

    Sub Price_Check()

    Dim i As Integer, SearchList As Range, Price As Currency
    SearchList = Range("R1:R5000")
    Price = 3.99
    i = 0
    For Each cell In SearchList
    i = i + 1
        If cell.Value = Price Then
            Cells.Value(2, i) = 2000
    End If
    Next cell

    End Sub

1 个答案:

答案 0 :(得分:1)

Sub Price_Check()

Dim i As Integer, SearchList As Range, Price As Currency
Dim sht As Worksheet, cell As Range

Set sht = ActiveSheet 'always best to specify a sheet, 
                      '  even if it's the default one

Set SearchList = sht.Range("R1:R5000") '<< added "Set"
Price = 3.99
i = 0
For Each cell In SearchList
    i = i + 1
    If cell.Value = Price Then
        sht.Cells(2, i).Value= 2000 '<< not Cells.Value(2, i)
                                    '  is i supposed to be the row though?
                                    '  Here it is the column
    End If
Next cell

End Sub