简体   繁体   English

VBA:在Excel中创建宏-编译错误-“未定义子或函数”

[英]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: 这是我的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 . 当前,我收到一个编译错误: 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. 注意:我认为该错误实际上是由continue关键字引起的。 Is there a continue keyword in VBA? VBA中有一个continue关键字吗?

continue is not used in VBA - you can instead wrap that if around the code in the main loop. continue在VBA中不使用-你可以代替换行if在主回路周围的代码。

break is not a VBA statement either - you probably want Exit in place of that. break也不是VBA语句-您可能希望使用Exit代替它。

In regards to your Update1, 关于您的Update1,

The problem is in your While loop. 问题出在您的While循环中。

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 更具体地说,在While之前添加Do ,然后将第一个Next更改为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) 但是您应该使用Do ... Loop结构(请单击示例链接)

Edit: To clarify, do not include the [] in your actual code. 编辑:为澄清起见,请勿在实际代码中包含[]。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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