Combine 2 VBA code

KlausW

Active Member
Joined
Sep 9, 2020
Messages
458
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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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