Solved

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

Posted on 2014-02-20
3
2,289 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

773 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