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!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

tonyyy

Well-known Member
Joined
Jun 24, 2015
Messages
1,709
Office Version
  1. 2010
Platform
  1. Windows
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
 

Forum statistics

Threads
1,147,997
Messages
5,744,259
Members
423,856
Latest member
kiaheyrati038

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top