SOLVED

Reset two MUTUALLY dependent drop down lists in Excel while avoiding a fatal loop crash

%3CLINGO-SUB%20id%3D%22lingo-sub-2744523%22%20slang%3D%22en-US%22%3EReset%20two%20MUTUALLY%20dependent%20drop%20down%20lists%20in%20Excel%20while%20avoiding%20a%20fatal%20loop%20crash%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2744523%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20preparing%20an%20Excel%20report%20schedule%20that%20contains%20some%20filters%20so%20the%20user%20can%20restrict%20the%20scope%20they%20want%20to%20see%2C%20and%20two%20of%20those%20filters%20are%20YEAR%20and%20MONTH%2C%20as%20the%20couple%20screen%20shots%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Year_Drop_Down.png%22%20style%3D%22width%3A%20358px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F309961iEDE416AD092C11F7%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Year_Drop_Down.png%22%20alt%3D%22Year_Drop_Down.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Month_Drop_Down.png%22%20style%3D%22width%3A%20356px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F309962iA6E2E41FCE42FD1D%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Month_Drop_Down.png%22%20alt%3D%22Month_Drop_Down.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wrote%20a%20named%20formula%20into%20Data%20Validation%20so%20every%20time%20a%20user%20selects%20a%20specific%20year%20on%20B3%2C%20the%20MONTH%20drop%20down%20below%20will%20show%20only%20those%20months%20related%20to%20that%20particular%20year.%20Conversely%2C%20every%20time%20a%20user%20selects%20a%20specific%20month%20on%20B4%2C%20the%20YEAR%20drop%20down%20above%20will%20show%20only%20that%20year%20related%20to%20that%20particular%20month%20that%20was%20selected.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20then%20I%20wrote%20that%20basic%20VBA%20code%20below%20so%20that%20every%20time%20the%20YEAR%20drop%20down%20on%20B3%20is%20erased%2C%20if%20there's%20anything%20already%20selected%20on%20the%20MONTH%20drop%20down%20on%20B4%20it%20will%20get%20automatically%20reset%20as%20well%2C%20in%20order%20to%20avoid%20inconsistencies%20of%20what%20is%20selected%20between%20the%20two%20cells%20(%3CU%3Ei.e.%3A%3C%2FU%3E%202021%20is%20selected%20in%20YEAR%20and%20Mar%2F2020%20is%20already%20selected%20in%20MONTH%2C%20which%20would%20generate%20a%20report%20error).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0A%0A%20%20%20%20If%20Target.Address%20%3D%20%22%24B%243%22%20Then%0A%0A%20%20%20%20%20%20%20%20Range(%22%24B%244%22).Value%20%3D%20%22%22%0A%20%20%20%20%0A%20%20%20%20End%20If%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%20I%20also%20need%20the%20other%20way%20around%20to%20happen%20too%3A%20reset%20what's%20on%20the%20YEAR%20filter%20in%20B3%20every%20time%20an%20user%20erases%20what's%20on%20the%20MONTH%20filter%20in%20B4.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20tweaking%20the%20above%20code%20to%20make%20that%20happen%2C%20but%20when%20I%20try%20to%20run%20it%20I%20either%20get%20a%20VBA%20code%20error%20or%20a%20fatal%20loop%20that%20ends%20up%20crashing%20and%20closing%20Excel.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20it%20possible%20to%20achieve%20what%20I%20want%3F%20And%20if%20so%2C%20what%20would%20be%20the%20best%20coding%20route%3F%20I'm%20really%20a%20newbie%20with%20VBA%2C%20so.....%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2744523%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2744865%22%20slang%3D%22en-US%22%3ERe%3A%20Reset%20two%20MUTUALLY%20dependent%20drop%20down%20lists%20in%20Excel%20while%20avoiding%20a%20fatal%20loop%20crash%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2744865%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1134010%22%20target%3D%22_blank%22%3E%40leolapa%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20section%20of%20code%20that%20resets%20the%20month%20triggers%20the%20section%20that%20resets%20the%20year%2C%20and%20that%20triggers%20the%20section%20that%20resets%20the%20month%2C%20etc.%2C%20causing%20an%20endless%20loop.%3C%2FP%3E%0A%3CP%3ETo%20prevent%20this%2C%20turn%20off%20event%20processing%20at%20the%20beginning%20of%20the%20code%2C%20and%20turn%20it%20on%20at%20the%20end%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0A%20%20%20%20Application.EnableEvents%20%3D%20False%0A%20%20%20%20If%20Not%20Intersect(Range(%22B3%22)%2C%20Target)%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20Range(%22B4%22).ClearContents%0A%20%20%20%20End%20If%0A%20%20%20%20If%20Not%20Intersect(Range(%22B4%22)%2C%20Target)%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20Range(%22B3%22).ClearContents%0A%20%20%20%20End%20If%0A%20%20%20%20Application.EnableEvents%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I'm preparing an Excel report schedule that contains some filters so the user can restrict the scope they want to see, and two of those filters are YEAR and MONTH, as the couple screen shots below:

 

Year_Drop_Down.png

 

Month_Drop_Down.png

 

I wrote a named formula into Data Validation so every time a user selects a specific year on B3, the MONTH drop down below will show only those months related to that particular year. Conversely, every time a user selects a specific month on B4, the YEAR drop down above will show only that year related to that particular month that was selected.

 

And then I wrote that basic VBA code below so that every time the YEAR drop down on B3 is erased, if there's anything already selected on the MONTH drop down on B4 it will get automatically reset as well, in order to avoid inconsistencies of what is selected between the two cells (i.e.: 2021 is selected in YEAR and Mar/2020 is already selected in MONTH, which would generate a report error).

 

 

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$B$3" Then

        Range("$B$4").Value = ""
    
    End If

End Sub

 

 

However, I also need the other way around to happen too: reset what's on the YEAR filter in B3 every time an user erases what's on the MONTH filter in B4.

 

I tried tweaking the above code to make that happen, but when I try to run it I either get a VBA code error or a fatal loop that ends up crashing and closing Excel.

 

Is it possible to achieve what I want? And if so, what would be the best coding route? I'm really a newbie with VBA, so.....

 

Thanks in advance

2 Replies
best response confirmed by leolapa (Occasional Contributor)
Solution

@leolapa 

The section of code that resets the month triggers the section that resets the year, and that triggers the section that resets the month, etc., causing an endless loop.

To prevent this, turn off event processing at the beginning of the code, and turn it on at the end:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Not Intersect(Range("B3"), Target) Is Nothing Then
        Range("B4").ClearContents
    End If
    If Not Intersect(Range("B4"), Target) Is Nothing Then
        Range("B3").ClearContents
    End If
    Application.EnableEvents = True
End Sub