我正在尝试将一个字符串数组作为变量传递给函数,当我尝试比较数组中的值时,我遇到' 424 Object required' 错误给定单元格中的值。我是VBA的新手,所以这可能是一个简单的语法错误,但我似乎无法弄明白。这是我的代码:
被调用的方法:
Sub InitializeCharts()
'Set's up the array for checking data names in the social groups
Dim socialArray As Variant
socialArray = Array("Chores", "Meat & Potatos", "Work", "Wind Down", "Reward")
'...
Call ChartLogic(Range("'ActivityTracker'!B12"), Range("'Groups'!F4"), socialArray)
End Sub
ChartLogic方法:
Sub ChartLogic(dataCell As Range, tableCell As Range, socialArray As Variant)
Dim temp As Double
Dim count As Integer
'...
'Loops through the table and looks for the social cells with the same name, adding them to the chart
Do Until IsEmpty(dataCell)
For count = LBound(socialArray) To UBound(socialArray)
If socialArray(count).Value = dataCell.Value Then '<---Error Here
temp = socialCell.Offset(count, 0).Value
socialCell.Offset(count, 0).Value = temp + dataCell.Offset(0, 4).Value
End If
Next
Set dataCell = dataCell.Offset(1, 0)
Loop
End Sub
提前致谢!
答案 0 :(得分:3)
您收到 Object required 错误,因为socialArray(count)
不会产生具有属性Value
的对象。
换句话说,由于socialArray
是Array
个字符串,socialArray(count)
已经产生了一个字符串 - 不需要Value
。
答案 1 :(得分:1)
正如Andrew指出的那样 - socialArray(count).Value =会导致错误,因为它是一个变体。您可以将它存储为这样的局部变量。
ArrVal = socialArray(count)
For count = LBound(socialArray) To UBound(socialArray)
ArrayVal = socialArray(count)
If ArrayVal = dataCell.Value Then '<---Error Here
temp = socialCell.Offset(count, 0).Value
socialCell.Offset(count, 0).Value = temp + dataCell.Offset(0, 4).Value
End If
Next
或者您可以取消.value,因为它不是单元格,不是工作表对象,而是变体。
If socialArray(count) = dataCell.Value Then