简体   繁体   中英

VBA: Creating Macro in Excel - Compile Error — 'Sub or function is not defined'

I have a workbook with several sheets with tables on them. I want to aggregate all the rows from the sheets onto the main table in the first. I would like it to be dynamic, so that when I add a row to one of the other sheets, it adds the row to the main table. Here is the code for my Sub:

Public Sub AggregateIssues() 
    For pgNum = 1 To ActiveWorkbook.Sheets.Count
        If ActiveWorkbook.Sheets(pgNum).Name = "Main" Then

          currSheet = ActiveWorkbook.Sheets(pgNum) 'Get Sheet
          flag = True
          RowIndex = 0
          While currSheet.Tables(0).Rows(RowIndex).Cells(0).Text = Null Or currSheet.Tables(0).Rows(RowIndex).Cells(0).Text = ""

            Row = currSheet.Tables(0).Rows(RowIndex)
            ActiveWorkbook.Sheets("Main").Tables("MainTbl").Append (Row)
            RowIndex = RowIndex + 1
          Next
        End If
    Next pgNum
End Sub

Currently, I am getting a compile error: Sub or function is not defined . The error is thrown on the name of the sub. This is the definition. Of course it is not defined yet. Any ideas on why this is happening?

Note: I believe the error is actually be caused by the continue keyword. Is there a continue keyword in VBA?

continue is not used in VBA - you can instead wrap that if around the code in the main loop.

break is not a VBA statement either - you probably want Exit in place of that.

In regards to your Update1,

The problem is in your While loop.

The correct format is this:

Do while [boolean expression]
'do something
Loop

To be more specific add Do before While , and change your first Next to Loop

Technically you can also use this structure which I like for nostalgic reasons:

While [boolean expression]
'do something
Wend

but you should use the Do...Loop structure (click link for examples)

Edit: To clarify, do not include the [] in your actual code. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM