2 SelectionChange on the same worksheet

Chuckarou

New Member
Joined
Jul 12, 2018
Messages
19
Hello all, I've found quite a lot of usefull tips on this forum by just looking around, but I can't seem to find this answer for this problem. Keep in mind that I have next to no knowledge in VBA of coding in general.

I have found two codes that I would like to use simultaneously on the same worksheet, but apparently I can't because they are both SelectionChange codes.

Here are the codes:

Code 1:

Private sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target,Range(“A:1”)) Is Nothing Then
If Target.Count > 1 Then Exit Sub
If Target.Value = “þ” Then Target.Value = “¨” Else: Target.Value = “þ”
End If
End Sub

Code 2:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static sRg As Range
Dim ColumnOffset As Integer
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Union([B:B], [D:D], [F:F])) Is Nothing Then
With Target
Application.EnableEvents = False
If Not sRg Is Nothing Then
If sRg.Column < .Column Then
ColumnOffset = 2
ElseIf .Column <> 1 Then
ColumnOffset = -1
End If
Else
ColumnOffset = 1
End If
.Offset(, ColumnOffset).Select
Application.EnableEvents = True
End With
End If
Set sRg = ActiveCell
End Sub

Thanks in advance for anyone who has answers!
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

tonyyy

Well-known Member
Joined
Jun 24, 2015
Messages
1,647
Chuckarou,

You might consider the following...

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A:1")) Is Nothing Then
    If Target.Count > 1 Then Exit Sub
    If Target.Value = "þ" Then Target.Value = "¨" Else: Target.Value = "þ"
End If

If Not Intersect(Target, Union([B:B], [D:D], [F:F])) Is Nothing Then
    If Target.Count > 1 Then Exit Sub
    Static sRg As Range
    Dim ColumnOffset As Integer
    With Target
        Application.EnableEvents = False
        If Not sRg Is Nothing Then
            If sRg.Column < .Column Then
                ColumnOffset = 2
            ElseIf .Column <> 1 Then
                ColumnOffset = -1
            End If
        Else
            ColumnOffset = 1
        End If
        .Offset(, ColumnOffset).Select
        Application.EnableEvents = True
    End With
    Set sRg = ActiveCell
End If
End Sub

Cheers,

tonyyy
 

Watch MrExcel Video

Forum statistics

Threads
1,109,073
Messages
5,526,655
Members
409,713
Latest member
roman9980

This Week's Hot Topics

Top