简体   繁体   English

选项显式,工作表更改事件,Excel VBA

[英]Options Explicit, worksheet change event, Excel VBA

I am new to Excel VBA, but I have some minor code expirence from Matlab and C++. 我是Excel VBA的新手,但是Matlab和C ++有一些次要的代码到期。

I have written the following code intended to display a msgbox-message if a certain cell is changed to contain a certain text string from a drop-down list: 我编写了以下代码,如果将某个单元格更改为包含下拉列表中的某些文本字符串,则打算显示msgbox消息:

Options Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$7" Then
        If Range("C7").Value = "Blue home" Then
        MsgBox "Hello world!"
        End If
    End If
End Sub

I get the following error message: Compile error: Invalid outside procedure 我收到以下错误消息:编译错误:无效的外部过程

When I remove options explicit from the code and turn off the require variable declaration options under tools in the VBE, the code works. 当我从代码中删除显式选项并关闭VBE中的工具下的require变量声明选项时,代码即可工作。 Thus the options explicit line triggers the problem. 因此,选项显式行触发了该问题。

What is causing problems with options explicit? 是什么导致显式选项出现问题? I cannot really figure out that there are any variables in my code that needs to be declared. 我真的无法弄清楚我的代码中是否需要声明任何变量。

Thanks for any help! 谢谢你的帮助!

Jeeped is correct in his comment you spelled it wrong it should look like below: 吉普(Jeeped)在您的注释中输入的拼写错误是正确的,如下所示:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$7" Then
        If Range("C7").Value = "Blue home" Then
        MsgBox "Hello world!"
        End If
    End If
End Sub

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

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