Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Combine two Worksheet_SelectionChange(ByVal Target As Excel.Range)

Posted on 2015-02-12
3
Medium Priority
?
107 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

688 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