Adding If...Then to my Worksheet_Change?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Good morning.

I have this code that runs a macro when the value in a specific column changes. I'd like to add an If...Then so the macro does NOT run if <ADD NEW> is the value. Here's my code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub

    On Error Resume Next
    
    Set KeyCells = Range("CalendarModelColumn")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
       
    Application.EnableEvents = False
                   
    Call Model
        
    Application.EnableEvents = True
    
    End If

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You should be able to modify your exit line like this:
Rich (BB code):
If Target.Cells.Count > 1 Or Target.HasFormula Or Target = "ADD NEW" Then Exit Sub

Just modify the text to match EXACTLY the way it looks in the cell.
 
Upvote 0
You should be able to modify your exit line like this:
Rich (BB code):
If Target.Cells.Count > 1 Or Target.HasFormula Or Target = "ADD NEW" Then Exit Sub

Just modify the text to match EXACTLY the way it looks in the cell.

That works nicely!

Can that same idea be applied here?:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Target.Calculate
       
    Application.OnKey "{DELETE}"
       
    Dim KeyCells As Range
       
    Set KeyCells = Range("CalendarModelColumn")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
    
    Application.EnableEvents = False
    
    Application.OnKey "{DELETE}", "DeleteModel"
    
    Application.EnableEvents = True
    
    End If
           
End Sub

I tried this but it doesn't work:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Target.Calculate
       
    Application.OnKey "{DELETE}"

    If Target = "<ADD NEW>" Then Exit Sub

    End If
       
    Dim KeyCells As Range
       
    Set KeyCells = Range("CalendarModelColumn")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
    
    Application.EnableEvents = False
    
    Application.OnKey "{DELETE}", "DeleteModel"
    
    Application.EnableEvents = True
    
    End If
           
End Sub
 
Upvote 0
Hmmm, that is really a different question altogether.
That code runs when a cell is selected (not changed).
Is the value in the cell selected really "<ADD NEW>"?

What is the purpose of the "Target.Calculate" line?
Do you not have automatic calculations turned on?
 
Upvote 0
Hmmm, that is really a different question altogether.
That code runs when a cell is selected (not changed).
Is the value in the cell selected really "<ADD NEW>"?

Could be. It's a drop down list. The first value in the list is <ADD NEW> which when selected opens a Userform. If the user selects <ADD NEW> and hits the Delete key I don't want the DeleteModel macro to run.
 
Upvote 0
It sounds to me like you do not need the "Worksheet_SelectionChange" code at all, which is triggered to run whenever a cell is just selected.
The "Worksheet_Change" code runs when the value is manually changed.

If I understand you correctly, it sounds to me like the "Worksheet_Change" is the only code you need here, and you can do away with the other code.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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