VBA Crashes Excel with Nested If Statements and Intersect

truebluewoman

New Member
Joined
Sep 26, 2014
Messages
36
I am trying to write VBA code that will change the values of specific rows based on what is entered within 3 columns of information. What I've written works, but then crashes the file every time. I don't know what I am doing wrong.

1. I have a named range of a group of columns and rows, "VAR_OPTIONS". The cells are '3-Other Personnel Exp'!$P$24:$R$30
2. I have drop-downs available for 2 of these columns (P and R). Column R is a number that someone has to type in.
3. I need that when someone changes any value within that range that it will perform a series of changes to the cells based on information entered within the same row, but in other columns.
3. Below is the code I have written. Every time it runs, it will perform the tasks, but then crashes.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
If Intersect(Target, Range("VAR_OPTIONS")) Is Nothing Then Exit Sub

If Sheets("3-Other Personnel Exp").Range("K" & Target.Row).Value = "Annual" And Sheets("3-Other Personnel Exp").Range("P" & Target.Row).Value = "Budgeted" Then
   Sheets("3-Other Personnel Exp").Range("Q" & Target.Row & ":R" & Target.Row).Locked = True
   Sheets("3-Other Personnel Exp").Range("Q" & Target.Row).Value = 1
 

ElseIf Sheets("3-Other Personnel Exp").Range("K" & Target.Row).Value = "Monthly" And Sheets("3-Other Personnel Exp").Range("P" & Target.Row).Value = "Budgeted" Then
    
    Sheets("3-Other Personnel Exp").Range("Q" & Target.Row & ":R" & Target.Row).Locked = True
    Sheets("3-Other Personnel Exp").Range("Q" & Target.Row).Value = 1
    Sheets("3-Other Personnel Exp").Range("R" & Target.Row).Value = "Monthly"


ElseIf Sheets("3-Other Personnel Exp").Range("K" & Target.Row).Value = "Variable" And Sheets("3-Other Personnel Exp").Range("P" & Target.Row).Value = "Budgeted" Then
    
    Sheets("3-Other Personnel Exp").Range("Q" & Target.Row).Locked = True
    Sheets("3-Other Personnel Exp").Range("Q" & Target.Row).Value = 1
    Sheets("3-Other Personnel Exp").Range("R" & Target.Row).Locked = False
 
ElseIf Sheets("3-Other Personnel Exp").Range("P" & Target.Row).Value = "Not Budgeted" Then
    
    Sheets("3-Other Personnel Exp").Range("Q" & Target.Row).Locked = True
    Sheets("3-Other Personnel Exp").Range("Q" & Target.Row).Value = 0
        
Else
    Exit Sub
 
End If
     
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The first thing that I notice is that your Change event is triggering other change events.
You might want to wrap your whole routine in Application.EnableEvents = False/True

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
If Intersect(Target, Range("VAR_OPTIONS")) Is Nothing Then Exit Sub

Application.EnableEvents = False

'....

Application.EnableEvents = True
End Sub
 
Upvote 0
Oh my goodness. That actually did it. I'm still learning VBA, so these are the kinds of things that I just do not know enough to look for and totally miss. Thank you SO much!!! You have no idea what having this fixed means to me. Have a wonderful evening.
 
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,653
Members
449,462
Latest member
Chislobog

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