您输入的名称无效“错误

时间:2015-07-31 21:32:58

标签: excel vba excel-vba

我定义了自己的简单Sub

Sub test1()
    MsgBox "Hello"
End Sub

但是当我尝试在单元格中引用它时

=test1()

我收到以下错误提示:

**The name that you entered is not valid
Reason for this:
1) The name does not begin with a letter or underscore
2) The name contains a space or other invalid character
3) The name conflicts with a Excel built in name or name of another object in the workbook**

我没有命名范围,宏在宏管理器窗口中运行没有问题。

**此错误消息的含义是什么?

我是否需要以某种方式命名宏以便能够使用它?**

1 个答案:

答案 0 :(得分:1)

两件事:

  • 首先,你的Sub必须是一个函数。
  • 其次,您需要将代码放在Module中,而不是放在任何Sheet或ThisWorkbook之后。