VBA Excel对象需要传递string数组variables

我试图将一个string数组作为variables传递给一个函数,当我尝试将数组中的值与给定单元格中的值进行比较时,我得到'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 

提前致谢!

正如Andrew指出的那样 – socialArray(count).Value =会导致错误,因为它是一个变体。 您可以将其存储为一个像这样的局部variables。

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 

你得到一个Object所需的错误,因为socialArray(count)不会产生具有Value属性的对象。

换句话说,由于socialArray是一个stringArraysocialArray(count)已经产生了一个string – 不需要Value