Solved

Combine two Worksheet_SelectionChange(ByVal Target As Excel.Range)

Posted on 2015-02-12
3
102 Views
Last Modified: 2016-02-11
Hi Experts

I have two Excel Private Sub Worksheet_SelectionChange that I'm trying to combine with no success.  Each work separately but I need to combine them.

The first is this:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Set Target = Range("name")
    If Target = "" Then Exit Sub
    ActiveSheet.Name = Left(Target, 31)
    Exit Sub
End Sub

The second is this:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("C6:C14")
   
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        overwrite
       
    End If
End Sub

The macro called overwrite is this:
Sub overwrite()
ActiveCell.Offset(-1, 1).Value = ActiveCell.Offset(-1, 1).Value
End Sub

So in trying to combine them, I tried this which doesn't work:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Set Target = Range("name")
    If Target = "" Then Exit Sub
    ActiveSheet.Name = Left(Target, 31)
    Exit Sub

    Dim KeyCells As Range
    Set KeyCells = Range("C6:C14")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
        overwrite
    End If
End Sub

What am I doing wrong?

Cheers,

Will
0
Comment
Question by:willnjen
[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 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 40607113
Try this...

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
     If Range("name") <> "" Then
        ActiveSheet.Name = Left(Range("name").Value, 31)
     End If
     If Not Application.Intersect(Target, Range("C6:C14")) Is Nothing Then
         overwrite
     End If
End Sub

Open in new window

0
 

Author Comment

by:willnjen
ID: 40607115
Thanks Wayne

I'll try that when I get back to my computer.

Cheers,

Will
0
 

Author Comment

by:willnjen
ID: 40611175
Hi Wayne

Thanks for your answer.  It works perfectly and the points are yours.

Cheers,

Will
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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…

710 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