【问题标题】:VBA script - Compile error: wrong number of argumentsVBA 脚本 - 编译错误:参数数量错误
【发布时间】:2015-11-13 14:58:13
【问题描述】:

我正在尝试创建一个基本的 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

【问题讨论】:

    标签: vba loops if-statement cell


    【解决方案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
    

    【讨论】:

    • 太棒了,非常感谢。是的,你也是正确的,我把行/列弄混了。更改后,它工作得很好。我不能超过你。
    猜你喜欢
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多