Clear dependent dropdown lists

Mylarbi

New Member
Joined
Feb 9, 2020
Messages
48
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi, I have a table with parent dropdown lists in columns 18, 30 and 34. Each one has a dependent dropdown lists in the second column to the right of each (that is columns 20, 32 and 36).
I want each dependent list cell to clear whenever its parent list is change. I have therefore created this code below in the sheet code of my macro-enabled files but it does seem to work. Kindly help.
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 18 Then
 If Target.Validation.Type = 3 Then
 Application.EnableEvents = False
 Target.Offset(0, 2).ClearContents
 End If
 End If
 On Error Resume Next
If Target.Column = 30 Then
 If Target.Validation.Type = 3 Then
 Application.EnableEvents = False
 Target.Offset(0, 2).ClearContents
 End If
End If
On Error Resume Next
If Target.Column = 34 Then
 If Target.Validation.Type = 3 Then
 Application.EnableEvents = False
 Target.Offset(0, 2).ClearContents
 End If
 End If
 End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi Mylarbi. Welcome to the forum

First things first. It's not a good idea to use On Error Resume Next. If you're getting errors, it's better to try and figure out what's causing them and fix it.

However, if you are going to use it you only need to enter it once at the beginning of your code. To revert back to normal you can use On Error Goto 0.

The same thing can be said for Application.EnableEvents = False. Just once at the beginning of your code is all it takes. Just remember to re-enable it at the end of your code with
Application.EnableEvents = True

Here's what your code should look like
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False
    
    If Target.Column = 18 Then
        If Target.Validation.Type = 3 Then
            Target.Offset(0, 2).ClearContents
        End If
    End If
    
    If Target.Column = 30 Then
        If Target.Validation.Type = 3 Then
            Target.Offset(0, 2).ClearContents
        End If
    End If
    
    If Target.Column = 34 Then
        If Target.Validation.Type = 3 Then
            Target.Offset(0, 2).ClearContents
        End If
    End If
    
    Application.EnableEvents = True
End Sub
 
Upvote 0
@juddaaaa Thanks for your help. I have used your suggested code. The function is a bit better than before but the main target to clear the dependent dropdown list is still not working. Please see
. Thanks
 
Upvote 0
@juddaaaa I just closed all applications and restarted my system. Now everything is working as expected with your code.
Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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