0

I'm trying to set a property of an object which is part of a class object array, for excel in VBA.

The code looks like this:

Dim myClass(5) as class1
Dim i as integer

For i = 0 to 5
    set myClass(i) = New class
    myClass(i).myProperty = "SomeValue"
Next i

Class code is simply:

Private pmyProperty as string

Public Property Let myProperty(s as string)
    pmyProperty = s
End Property
Public Property Get myProperty() as string
    myProperty = pmyProperty
End Property

However when I run this, I get a compile error "expected: list separator." This hits on the myClass(i).myProperty = "SomeValue" line.

How do I set the value of a property of an class object that is part of an array?

Any help would be great!


So the actual code is as follows...

Module code:

Public Sub main_sb_BillingApp()


    Dim intCountComplete As Integer
    Dim intLastRow As Integer
    Dim Line() As clsLine
    Dim i As Integer, x As Integer

    intCountComplete = WorksheetFunction.CountIf(Sheets(WS_NAME).Columns(COL_W_COMPLETE), "Yes")
    intLastRow = Sheets(WS_NAME).Cells(LAST_ROW, COL_W_COMPLETE).End(xlUp).Row - 1

    ReDim Line(intCountComplete - 1)

    For i = ROW_W_HEADER + 1 To intLastRow

        If Sheets(WS_NAME).Cells(i, COL_W_COMPLETE) = "Yes" Then

            Set Line(x) = New clsLine
            Line(x).Row = i
            x = x + 1

        End If

    Next i

End Sub

Class code:

Private pDate As Date
Private pUJN As String
Private pDesc As String
Private pCharge As Currency
Private pCost As Currency
Private pMargin As Double
Private pComplete As Boolean
Private pRow As Integer

Public Property Let Row(i As Integer)
    pRow = i
    Update
End Property
Public Property Get Row() As Integer
    Row = pRow
End Property

Private Sub Update()

    With Sheets(WS_NAME)

        pDate = .Cells(pRow, COL_W_DATE)
        pUJN = .Cells(pRow, COL_W_UJN)
        pDesc = .Cells(pRow, COL_W_DESC)
        pCharge = .Cells(pRow, COL_W_CHARGE)
        pCost = .Cells(pRow, COL_W_COST)
        pMargin = .Cells(pRow, COL_W_MARGIN)

        If .Cells(pRow, COL_W_COMPLETE) = "Yes" Then
            pComplete = True
        Else
            pComplete = False
        End If

    End With
End Sub
10
  • 2
    Is it VB.NET or vba or vbscript? Commented Feb 20, 2017 at 15:33
  • show relevant class code Commented Feb 20, 2017 at 15:34
  • The code I'm using is VBA for excel. Commented Feb 20, 2017 at 15:48
  • Added the class code. Commented Feb 20, 2017 at 15:48
  • 1
    Your actual code also compiles cleanly for me. Commented Feb 20, 2017 at 15:59

1 Answer 1

4

Line is a VBA reserved keyword, so you're confusing the compiler. Change the name of your object array and it works just fine:

Dim lineArray() As clsLine
'...

        Set lineArray(x) = New clsLine
        lineArray(x).Row = i    
Sign up to request clarification or add additional context in comments.

1 Comment

Changed the name and worked perfectly! Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.