Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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,504 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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.

618 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