Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 111
  • Last Modified:

Combine two Worksheet_SelectionChange(ByVal Target As Excel.Range)

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
willnjen
Asked:
willnjen
  • 2
1 Solution
 
Wayne Taylor (webtubbs)Commented:
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
 
willnjenAuthor Commented:
Thanks Wayne

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

Cheers,

Will
0
 
willnjenAuthor Commented:
Hi Wayne

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

Cheers,

Will
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now