Omega
Omega

Reputation: 185

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

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

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.

Upvotes: 1

Views: 9162

Answers (2)

RubberDuck
RubberDuck

Reputation: 12728

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

If ActiveCell.Address = "A1" Then

But I believe address returns an absolute reference, so

If ActiveCell.Address = "$A$1" Then

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

  1. Use Option Explicit in 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.

Upvotes: 2

Paresh J
Paresh J

Reputation: 2419

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

Check this code:

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

Upvotes: 3

Related Questions