Solved

Excel VBA Worksheet Change Event triggerred by Data Validation selection - apply to non-contiguous ranges

Posted on 2014-02-20
3
2,268 Views
Last Modified: 2014-02-20
I am running a worksheet change event where if the selection from a data validation drop down list is changed then the cell immediately to the left is cleared.  I have it working just fine for cell C10 but i want it to apply to 4 non contiguous ranges and not just that single cell
C10:C17, C26:C33; C49:C56; C66:73
Probably something simple such as is target in this range.....cant get it right

My code is below:


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$10" Then
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With

    ActiveCell.Offset(0, 1).ClearContents

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
End If
End Sub
0
Comment
Question by:JohnNZExcel
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 200 total points
ID: 39873198
Change this line-->

If Target.Address = "$C$10" Then

Open in new window


To this-->

If Intersect(Target, Me.Range("C10:C17,C26:C33, C49:C56, C66:C73")) Is Nothing Then Exit Sub

Open in new window


And remove the last End if...You wont need it anymore..

Also one small observation you might want to change this line as well-->

ActiveCell.Offset(0, 1).ClearContents

Open in new window


To this-->

Target.Offset(0, 1).ClearContents

Open in new window


Since once you made changes the active cell will be the cell below it when macro is trigged not the same cell where you made changes...

Saurabh...
0
 

Author Comment

by:JohnNZExcel
ID: 39875529
Thank you Saurabh both for the fix and the advice re the Offset line later in the module.  I have tested it and everything works great.
0
 

Author Closing Comment

by:JohnNZExcel
ID: 39875530
Nice clean fix and easy to understand
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
This article will show you how to use shortcut menus in the Access run-time environment.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now