?
Solved

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

Posted on 2014-02-20
3
Medium Priority
?
2,413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 800 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

777 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