Combine 2 VBA code

KlausW

Active Member
Joined
Sep 9, 2020
Messages
453
Office Version
  1. 2016
Platform
  1. Windows
Hi every one

Can you combine these 2 VBA codes, so when I press cell in column J it shot run the first VBA-code and when I press in the cells in column K it shot run the secund VBA-code.

Some who can help.

Any help will be appreciated

Best Regards

Klaus W

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Code 1
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("J2:J41")) Is Nothing Then
            Range("ab16").Value = Target.Offset(0, -9).Value
            Target.Value = "S"
        End If
    End If
End Sub

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Code 2
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("k2:k41")) Is Nothing Then
            Range("ab18").Value = Target.Offset(0, -10).Value
            Target.Value = "G"
        End If
    End If
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Just place one under the other, i.e.
Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("J2:J41")) Is Nothing Then
            Range("ab16").Value = Target.Offset(0, -9).Value
            Target.Value = "S"
        End If
        If Not Intersect(Target, Range("k2:k41")) Is Nothing Then
            Range("ab18").Value = Target.Offset(0, -10).Value
            Target.Value = "G"
        End If
    End If
End Sub
 
Upvote 0
Solution
Just place one under the other, i.e.
Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("J2:J41")) Is Nothing Then
            Range("ab16").Value = Target.Offset(0, -9).Value
            Target.Value = "S"
        End If
        If Not Intersect(Target, Range("k2:k41")) Is Nothing Then
            Range("ab18").Value = Target.Offset(0, -10).Value
            Target.Value = "G"
        End If
    End If
End Sub
Good morning Joe4 1000 thanks it's as it should be. Good weekend from Denmark KlausW
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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
Back
Top