How to combine 2 vba codes in one

hendrikbez

Board Regular
Joined
Dec 13, 2013
Messages
95
Office Version
  1. 2021
Platform
  1. Windows
I am new to VBA in excel

This first part of the vba code worked, but I have changed it, as when run it and have a enrty with same name on column B it show the same name that it fitst get,
so now I have make 2 columns, but doi not know how to change it

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim Dest As Range
  If Target.CountLarge > 1 Then Exit Sub
  Set Target = Intersect(Target, Range("I:I"))
  If Target Is Nothing Then Exit Sub
  Set Dest = Range("B:B").Find(Target, LookIn:=xlValues, Lookat:=xlWhole)
  If Dest Is Nothing Then
    MsgBox Target, vbInformation, "Nie Gevind"
  Else
    Application.EnableEvents = False
    Dest.Select
    Application.EnableEvents = True
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim Dest As Range
  If Target.CountLarge > 1 Then Exit Sub
  Set Target = Intersect(Target, Range("J:J"))
  If Target Is Nothing Then Exit Sub
  Set Dest = Range("E:E").Find(Target, LookIn:=xlValues, Lookat:=xlWhole)
  If Dest Is Nothing Then
    MsgBox Target, vbInformation, "Nie Gevind"
  Else
    Application.EnableEvents = False
    Dest.Select
    Application.EnableEvents = True
  End If
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
What version of Excel are you using (please also change your Account Profile to show that) ?
If you are on MS365 or on 2021 are all your users on that version ie can we use XLookup.
 
Upvote 0
Here is another version using Find.
At the moment if there is only one instance in column B then both column I and J will go there.
If there are 2 instances in column B, Column I will go to the 1st and column J to the second.

Let me know if you want it to return not found if you select column J and there is only one occurence in column B.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim Dest As Range
  If Target.CountLarge > 1 Then Exit Sub
  Set Target = Intersect(Target, Range("I:J"))
  If Target Is Nothing Then Exit Sub

  Set Dest = Range("B:B").Find(Target, LookIn:=xlValues, Lookat:=xlWhole)
 
  If Target.Column = 10 Then
    Set Dest = Range("B:B").FindNext(after:=Dest)
  End If

  If Dest Is Nothing Then
    MsgBox Target, vbInformation, "Nie Gevind"
  Else
    Application.EnableEvents = False
    Dest.Select
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
Solution
Here is another version using Find.
At the moment if there is only one instance in column B then both column I and J will go there.
If there are 2 instances in column B, Column I will go to the 1st and column J to the second.

Let me know if you want it to return not found if you select column J and there is only one occurence in column B.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim Dest As Range
  If Target.CountLarge > 1 Then Exit Sub
  Set Target = Intersect(Target, Range("I:J"))
  If Target Is Nothing Then Exit Sub

  Set Dest = Range("B:B").Find(Target, LookIn:=xlValues, Lookat:=xlWhole)
 
  If Target.Column = 10 Then
    Set Dest = Range("B:B").FindNext(after:=Dest)
  End If

  If Dest Is Nothing Then
    MsgBox Target, vbInformation, "Nie Gevind"
  Else
    Application.EnableEvents = False
    Dest.Select
    Application.EnableEvents = True
  End If
End Sub
Thank you Alex, is is working fine for me now, and thank you for your patience with me.
 
Upvote 0
No problem. Glad we could help.
PS: Why "Nie" and not "Niet" ? or is it not Dutch ;)
I am Afrikaans Niet is for Dutch, I can understand if someone is typing text and when a dutch person speaks slowly, i can understand him/her
 
Upvote 0
I thought that was the most likely. My Dutch is very rusty though and not up to an actual conversation.
 
Upvote 0
This works
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim Dest As Range
  If Target.CountLarge > 1 Then Exit Sub
  If (Target.Column = 9 Or Target.Column = 10) And Target <> "" Then
  
  If Not Intersect(Target, Range("I:I")) Is Nothing Then
  Set Dest = Range("B:B").Find(Target, LookIn:=xlValues, Lookat:=xlWhole)
  ElseIf Not Intersect(Target, Range("J:J")) Is Nothing Then
  Set Dest = Range("E:E").Find(Target, LookIn:=xlValues, Lookat:=xlWhole)
  End If
    
  If Dest Is Nothing Then
    MsgBox Target, vbInformation, "Nie Gevind"
  Else
    Application.EnableEvents = False
    Dest.Select
    Application.EnableEvents = True
  End If
  End If
End Sub
 
Upvote 0
This works
It does not take into account the additional information provided. Both find statements need to be looking at column B (not column E) and if the Target is in Column J ideally the 2nd occurence (should it exist) in column B should be selected.
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

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