none
Compile Error trying to define structures RRS feed

  • Question

  • My workbook has a module "Code" in which I am trying to define some structures. The relevant parts of the module are:

    Option Explicit


    Public Structure NbkGiver
        Public name As String
        Public Amount As Currency
    End Structure   'NbkGiver

    Public Structure NbkFund
        Public name As String
        Public Givers As Integer                                                'Actual number of givers for this fund.
        Public RowSize As Integer                                               'Rows required to tabulate this fund.
        Public Style As Integer
        Public Seq As Integer
        Public FirstRow As Integer
        Public Fixed As Boolean                                                 'Fund start row has been finalised.
        Public Giver() As NbkGiver
    End Structure   'NbkFund

    Public Structure NbkSection
        Public Funds As Integer
        Public AllGivers As Integer
        Public RowSize As Integer                                               'Current best (smallest) size of section in rows.
        Public RowTsize As Integer                                              'Trial size while investigating a new sequence.
        Public FundSeq() As Integer                                             'Current best sequence of funds.
        Public FundTseq() As Integer                                            'Trial sequence.
        Public Fund() As NbkFund
        Public Data As Variant                                                  'Range accepting data from the worksheet.
    End Structure   'NbkSection

    Sub InitOrder(ByRef sec As NbkSection)
        Dim f As Integer

        With sec
            .RowSize = MaxInt                                               'Initialise the section's overall size.

            For f = 1 To .Funds
                .FundTseq(f) = f
            Next f
        End With    'sec
    End Sub 'InitOrder(NbkSection)

    Function NextOrder(ByRef sec As NbkSection) As Boolean

        Dim t As Integer
        Dim t1 As Integer
        Dim t2 As Integer
        Dim q As Integer
        Dim r As Boolean

        With sec

            For t = .Funds To 1 Step -1                                     'Start with highest element.
    again:
                If .FundTseq(t) < .Funds Then

                    q = .FundTseq(t) + 1
                    .FundTseq(t) = q + 1                                    'Try incrementing it.

                    For t1 = 1 To t - 1
                        If .FundTseq(t1) = q Then
                            GoTo again                                      'New value already used - try incrementing it again.
                        End If
                    Next t1

                    q = 0                                                   'Prototype value for resetting.
                    For t1 = t + 1 To .Funds                                'For each subsequent element.
    newq:
                        q = q + 1                                           'Try a new prototype value.
                        For t2 = 1 To t
                            If .FundTseq(t2) = q Then
                                GoTo newq                                   'That value is already in use, try the next value.
                            End If
                        Next t2
                        .FundTseq(t1) = q                                   'We always get here eventually to do the reset.
                    Next t1

                    NextOrder = True
                    Exit Function
                End If                                                      'End If .FundTseq(t) < .Funds

            Next t

            NextOrder = False
        End With    'sec
    End Function    'NextOrder(NbkSection)

    Sub debugNextOrder()

    Dim i As Integer
    Dim testsec As NbkSection

    With testsec

        .Funds = 4
        ReDim .FundTseq(.Funds)

        InitOrder (testsec)

        Do
            Debug.Print .FundTseq(1); .FundTseq(2); .FundTseq(3); .FundTseq(4); vbCrLf
        Loop While NextOrder(testsec) = True

    End With    'testsec

    End Sub 'debugNextOrder

    When I try to test "Sub debugNextOrder()" (by placing the cursor on that line and pressing F5), I get a Msgbox:

                        Compile Error: Expected: end of statement

    and the lines "Public Structure..." and "End Structure" of all three structures are highlighted in red. But I think I am doing everything according to the book (cf. msdn.microsoft.com/en-us/library/4ft0z102.aspx and related webpages.)

    What am I doing wrong?

    Wednesday, March 2, 2016 6:44 PM

Answers

All replies

  • Structures are used in Visual basic. In VBS use Type as in:

    Option Explicit

    Type test
        Str As String
        lng As Long
    End Type

    Sub testme()
    Dim tst As test
        tst.Str = "Hello World"
        tst.lng = 1
        Debug.Print tst.Str
        Debug.Print tst.lng
    End Sub


    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

    Thursday, March 3, 2016 12:39 AM
  • Thanks, Rod

    Now I'm getting "Compile error: Variable required - can't assign to this expression" on the line

            "InitOrder(testsec)"

    I've replaced "Structure" with "Type" in the six places where the word occurs near the start of the file. I've also found buried in the file a number of red-inked lines, which I've corrected over several iterations. [It seems that the place where VB appears to be flagging an error, and the actual place(s) in error, may be quite different.] In particular, I removed "Public" from the internal lines of the Type declarations.

    To recap, we have:

        Public Type NbkSection
            Funds As Integer
            'etc.
        End Type   'NbkSection

        Sub InitOrder(ByRef sec As NbkSection)
            Dim f As Integer

            With sec
               .RowSize = MaxInt                                               'Initialise the section's overall size.

                For f = 1 To .Funds
                    .FundTseq(f) = f
                Next f
            End With    'sec
        End Sub 'InitOrder(NbkSection)

        Sub debugNextOrder()

        Dim i As Integer
        Dim testsec As NbkSection

        With testsec

            .Funds = 4
            ReDim .FundTseq(.Funds)

            InitOrder (testsec)

            Do
                Debug.Print .FundTseq(1); .FundTseq(2); .FundTseq(3); .FundTseq(4); vbCrLf
            Loop While NextOrder(testsec) = True

        End With    'testsec

        End Sub 'debugNextOrder

    Thursday, March 3, 2016 2:12 PM
  • Change it to:

            Call InitOrder(testsec)


    I think it is getting confused because you use testsec in the call and you are in the With testsec.
    • Edited by mogulman52 Thursday, March 3, 2016 3:18 PM
    • Marked as answer by OldColdDreamer Thursday, March 3, 2016 11:44 PM
    Thursday, March 3, 2016 3:02 PM
  • Thanks, mogulman52 (and again to Rod).

    That now runs. It's got bugs in it, but nothing I can't now sort out, and they are nothing to do with the impasse that I had been facing. So I consider my question satisfactorily answered.

    Thursday, March 3, 2016 11:51 PM