selection.change event refuses to execute

littlepete

Well-known Member
Joined
Mar 26, 2015
Messages
507
Office Version
  1. 365
Platform
  1. Windows
hello :)

I have an excel sheet to type in my lottery numbers ...
all works excellent, but i'm trying to 1. clear all data and 2. start the check the numbers macro with an selection.change event...
selected from the macro list, both macros work as they should...

the macro to clear all data is on range c3:f3, named range "knopvoor" (preparation)
the macro to check all the numbers is on range h3:k3, named range "knoptrek" (choose)

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("knopvoor")) Is Nothing Then
            Call voorbereiding
        End If
        If Not Intersect(Target, Range("knoptrek")) Is Nothing Then
            Call trekking
        End If
    End If
End Sub

what could I be doing wrong ?
thanks for the help !
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Do you have that code in a sheet module or a separate module? The separate module is the wrong choice. ;)
 
Upvote 0
Do you have that code in a sheet module or a separate module? The separate module is the wrong choice. ;)
hello :)

that sub... end sub is placed in sheet one called trekking...
the only thing i doubted about was the merged cells: the two buttons to click are both a merge of four cells
knopvoor = c3:f3 and knoptrek = h3:k3
but also without merged cells no result; also with the cells and not named range no result...
 

Attachments

  • 2021-12-26.png
    2021-12-26.png
    216.2 KB · Views: 24
Upvote 0
Instead of If Selection.Count = 1 Then, you might be better off with
If Target.MergeCells Then

But as @MARK858 alluded to, why are using a SelectionChange event using pretend buttons based on Merged Cells ?
Selection change is going to run every single time you move from one cell to another.
If you want a button anyway just create a button linked to the procedure you are calling anyway.That way it will only run when you click on the button.
 
Upvote 0
so, can i ask, without creating a button, but with an event, what should i put in the sheet code?
there is a choice:
1. clicking a cell to start the macro; or:
2. starting the lottery calculation after filling in the seventh number...

what should i write then?

answers:
the two "buttons": are the cells on the picture left top corner (cells in row 3).

i guessed the event would only work when i click one of those two cells, not selecting any cell ?
 
Upvote 0
with an event, what should i put in the sheet code?
there is a choice:
1. clicking a cell to start the macro;

I can't see the benefit in having a Cell (pretend) button over a real button (actually I can only see disadvantages) having said that just changing your if statement as previously advised should work.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If If Target.MergeCells Then              ' Changed Line
        If Not Intersect(Target, Range("knopvoor")) Is Nothing Then
            Call voorbereiding
        End If
        If Not Intersect(Target, Range("knoptrek")) Is Nothing Then
            Call trekking
        End If
    End If
End Sub

2. starting the lottery calculation after filling in the seventh number...
Instead of SelectionChange use Worksheet_Change.
Check for an intersect with any 1 of the 7 (merged) entry cells.
Then count all 7 entry cells for numerics and if it comes to 7 execute the code.
 
Upvote 0
It is probably not getting past the
VBA Code:
If Selection.Count = 1 Then
line.

What are you trying to test with that line?


What 2 buttons?
yeahhh !

removing the selection 1 line did the job :) !!! thank you for the suggestions !!!

now, next job: win the lottery hahaha !!!
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,143
Members
453,021
Latest member
Justyna P

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