Alex
Alex

Reputation: 150

VBA Excel object required passing string array variable

I am attempting to pass an array of strings into a function as a variable and am getting a '424 Object required' error when I try to compare the values in the array to values in a given cell. I am new to VBA so this may be a simple syntax error but I cannot seem to figure it out. Here's my code:

Method being called:

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 Method:

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

Thanks in advance!

Upvotes: 0

Views: 3917

Answers (2)

Steve Kline
Steve Kline

Reputation: 805

As Andrew pointed out - socialArray(count).Value = will cause an error because it's a variant. You can store it as a local variable like this.

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

Or you could just take off the .value as it's not a cell and is not a worksheet object but a variant.

If socialArray(count) = dataCell.Value Then

Upvotes: 1

Andrew Cheong
Andrew Cheong

Reputation: 30273

You're getting an Object required error because socialArray(count) does not yield an object that has the property Value.

In other words, since socialArray is an Array of strings, socialArray(count) already yields a string—there's no need for Value.

Upvotes: 4

Related Questions