Solved

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

Posted on 2014-02-20
3
2,254 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
Comment Utility
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
Comment Utility
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
Comment Utility
Nice clean fix and easy to understand
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Outlook Free & Paid Tools
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

771 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

10 Experts available now in Live!

Get 1:1 Help Now