# Run code if a cell is selected



## wwisconsin1 (Dec 28, 2022)

I want to activate some code when a particular cell is selected.
I found this code below from Teylan in New Zealand posted in 2013.
When I put this code in the sheet module it works perfectly. Thankyou Teylan.

But instead of cell D4 as the activation cell, I want to use merged cells D4 and D5 as the activation cell.

I tried changing the code line to this:
(Target, Range("D4:D5")) but it doesn't work. Any suggestions would be appreciated.
Thank you

'''Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("D4")) Is Nothing Then
            Application.Calculate
        End If
    End If
End Sub'''


----------



## Joe4 (Dec 28, 2022)

Welcome to the Board!

If you have merged cells D4 and D5, you will need to change this line:

```
If Selection.Count = *1* Then
```
to this:

```
If Selection.Count = *2 *Then
```
though I would STRONGLY recommend not using merged cells if at all possible.
Merged cells the probably the single worst feature of Excel and cause all sorts of issues for things like VBA and sorting, among other things.
Most experienced Excel programmers avoid them like the plague!


----------



## wwisconsin1 (Dec 29, 2022)

Joe4 said:


> Welcome to the Board!
> 
> If you have merged cells D4 and D5, you will need to change this line:
> 
> ...


That did the trick. Thankyou.
The two merged cells are only a text entry that says "ENTER".  It activates simple code that simulates pressing the F9 button. After typing in a letter in each of 5 other cells you hit enter.  I need this because calculation time is slow, so I have the calculation mode on Manual. Now I'm trying to figure out why calculations are taking so long.  I could have also done this with a shape that says "ENTER" and assign the shape to a macro.
Thanks again.


----------

