vba 如何在excel宏中检查activecell是否为A1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27037578/
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 05:19:10  来源:igfitidea点击:

how to check whether activecell is A1 or not in excel macro?

excelvbaexcel-vba

提问by Omega

I'm new to Excel Macros. I have been trying to find whether the active cell is A1 or not. I tried

我是 Excel 宏的新手。我一直在试图找出活动单元格是否为 A1。我试过

if Activecell.Address = A1 then
      Msgbox(.....)

I also tried

我也试过

if Activecell.Name = A1 then
      Msgbox(.....)

These two were not working. Any help would be appreciated.

这两个没有工作。任何帮助,将不胜感激。

回答by Paresh J

Arun, What you have done is not wrong. Your code needs to be changed a bit.

阿伦,你所做的没有错。您的代码需要稍作更改。

Check this code:

检查此代码:

If ActiveCell.Address = "$A" Then
    MsgBox "You Selected Cell A1."
End If

回答by RubberDuck

The Addressis a string. In the example you've shown A1is a variable of some type. Strings get wrapped in quotes.

Address是一个字符串。在您显示的示例中,A1是某种类型的变量。字符串被包裹在引号中。

If ActiveCell.Address = "A1" Then

But I believe address returns an absolute reference, so

但我相信地址返回一个绝对引用,所以

If ActiveCell.Address = "$A" Then


Now, you probably could have worked this out yourself if you had done a few things.

现在,如果你做了一些事情,你可能会自己解决这个问题。

  1. Use Option Explicitin all of your code modules. It forces you to declare your variables, which would have alerted you to the first issue.
  2. On the menu bar, go to View >> Immediate. Make sure you have a workbook open and a cell selected, then type the following into the Immediate Window.

    ?ActiveCell.Address
    

    Then press ENTER.

  1. 使用Option Explicit在所有的代码模块。它强制您声明变量,这会提醒您注意第一个问题。
  2. 在菜单栏上,转到查看 >> 立即。确保您打开了一个工作簿并选择了一个单元格,然后在“立即窗口”中键入以下内容。

    ?ActiveCell.Address
    

    然后按ENTER