Adding If...Then to my Worksheet_Change?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
362
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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,515
Office Version
  1. 365
Platform
  1. Windows
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.
 

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
362
Office Version
  1. 365
Platform
  1. Windows
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,515
Office Version
  1. 365
Platform
  1. Windows
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?
 

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
362
Office Version
  1. 365
Platform
  1. Windows
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.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
65,515
Office Version
  1. 365
Platform
  1. Windows
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.
 

Forum statistics

Threads
1,176,287
Messages
5,902,328
Members
434,962
Latest member
sgilmoreBBP

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
Top