Run code if a cell is selected

wwisconsin1

New Member
Joined
Dec 28, 2022
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
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'''
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to the Board!

If you have merged cells D4 and D5, you will need to change this line:
Rich (BB code):
If Selection.Count = 1 Then
to this:
Rich (BB code):
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!
 
Upvote 0
Solution
Welcome to the Board!

If you have merged cells D4 and D5, you will need to change this line:
Rich (BB code):
If Selection.Count = 1 Then
to this:
Rich (BB code):
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!
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.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top