vba 编译错误预期函数或变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23274927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:52:43  来源:igfitidea点击:

Compile Error Expected Function Or Variable

excelvbaexcel-vba

提问by user3569764

I'm new and trying to learn VBA. When I'm typing in the code I get Compile Error Expected Function Or Variable.

我是新手,正在尝试学习 VBA。当我输入代码时,我得到编译错误预期函数或变量。

Is something regarding the activecell, but can't figure it out.

是关于activecell的东西,但无法弄清楚。

Sub Testare()
    Dim FilmName As String
    Dim FilmLenght As Integer
    Dim FilmDescription As String

    Range("b10").Select
    FilmName = ActiveCell.Value
    FilmLenght = ActiveCell.Offset(0, 2).Value

    If FilmLenght < 100 Then
       FilmDescription = "Interesant"
    Else
       FilmDescription = "Suficient"
    End If

    MsgBox FilmName & " is " & FilmDescription
End Sub

回答by Artur Rutkowski

This error happens also when a Sub is called the same as variable (i.e. in one Sub you have for loop with iterator "a", whilst another Sub is called "a").

当 Sub 被称为与变量相同的调用时也会发生此错误(即在一个 Sub 中,您有带有迭代器“a”的 for 循环,而另一个 Sub 被称为“a”)。

This gives an error that fits to your description.

这给出了符合您描述的错误。

Regards

问候

回答by Gary's Student

It is possible to make your code fail in two different ways:

有可能以两种不同的方式使您的代码失败:

  1. Place a very large value in D10
  2. Place a text value in D10
  1. D10 中放置一个非常大的值
  2. D10 中放置一个文本值

This will result in either an overflow error or type mismatch error.

这将导致溢出错误或类型不匹配错误。

回答by Peter Rush

I know this was asked a while ago, but I ended up with the same error when I created a Sub within a worksheet with the Sub Name the same as the Worksheet name.

我知道这是不久前被问到的,但是当我在工作表中创建一个子名称与工作表名称相同的子时,我最终遇到了同样的错误。

Unfortunately when this happens, no error is highlighted by a compile; the error only appears at run time. Also, the offending line is not highlighted, which makes it a bit difficult to find.

不幸的是,当发生这种情况时,编译不会突出显示错误;该错误仅在运行时出现。此外,违规行没有突出显示,这使得它有点难以找到。

Changing the Sub name solves it though.

更改子名称可以解决它。

回答by cssyphus

Here is what caused this error in my case:

以下是在我的情况下导致此错误的原因:

Inside a new Public Function that I was starting to write, I began to type a statement, but needed to copy a long varname from another location, so I just inserted the letter ato prevent Excel from popping-up the uber-annoying Compile Error: Expected Expressiondialog. So I had this:

在我开始编写的一个新的公共函数中,我开始输入一个语句,但需要从另一个位置复制一个长 varname,所以我只是插入了字母a以防止 Excel 弹出超级烦人的Compile Error: Expected Expression对话框。所以我有这个:

myVarName = a

I then attempted to step-through my code to get to that point, and when Excel entered that function I received the message "Compile Error: Expected Function Or Variable". To resolve, I just changed the placeholder to a number:

然后我尝试逐步执行我的代码以达到这一点,当 Excel 输入该函数时,我收到了消息"Compile Error: Expected Function Or Variable"。为了解决,我只是将占位符更改为一个数字:

myVarName = 1

And all continued just fine.

一切都很好。