multiple Target Ranges

Mohamedazees

New Member
Joined
Oct 18, 2020
Messages
43
Office Version
  1. 2019
Platform
  1. Windows
Dear Sir,

I need to apply target for multiple ranges in a same sheet refer the below code i have applied Target for Column "G" like wise I need to target Column N & T accordingly do VLOOKUP's like below with difference ranges
Thanks in advance
Code:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Target, [G:G]) Is Nothing Or Target.Count > 1 Then Exit Sub
'Target.Offset(, -1) = Time
Target.Offset(, 1) = Application.VLookup(Target, Sheet10.Range("A:B"), 2, False)

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Count > 1 Then Exit Sub
If (Intersect(Target, [G:G]) Is Nothing) And (Intersect(Target, [N:N]) Is Nothing) And (Intersect(Target, [T:T]) Is Nothing) Then Exit Sub
'Target.Offset(, -1) = Time
Target.Offset(, 1) = Application.VLookup(Target, Sheet10.Range("A:B"), 2, False)

End Sub
 
Upvote 0
Dear Sir,
Thanks for your prompt reply
Sorry I fell I am not Properly explain my requirement

VBA to do :

1. When Entering value in Column Range "G" then offset column "H" will populate the Lookup Value
2. When Entering value in Column Range "N" then offset column "W" will populate the Lookup Value ( Another Lookup Value)
3. When Entering value in Column Range "T" then offset column "X" will populate the Lookup Value ( Another Lookup Value)

Thanks for your guldens & supports
 
Upvote 0
Since the number of columns you are offsetting by is not the same for all three columns, I recommend you split them up into their own sections.

I have done the first one for you, you just need to enter the appropriate VLOOKUP formula for the other two.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Count > 1 Then Exit Sub

'   Column G check
    If Not (Intersect(Target, [G:G]) Is Nothing) Then
       Target.Offset(, 1) = Application.VLookup(Target, Sheet10.Range("A:B"), 2, False)
    End If
   
'   Column N check
    If Not (Intersect(Target, [N:N]) Is Nothing) Then
'       Enter your vlookup formula here
    End If
   
'   Column T check
    If Not (Intersect(Target, [T:T]) Is Nothing) Then
'       Enter your vlookup formula here
    End If

End Sub
 
Upvote 0
Solution
Hopefully I haven't confused you doing it this way.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim Target As Range
    Set Target = ActiveCell
    
    Dim rng As Range
    Set rng = Range("G:G,N:N,T:T")
    
    If Not Intersect(Target, rng) Is Nothing And Target.Count = 1 Then
        Select Case Target.Column
            Case Columns("G").Column
                Debug.Print "G Lookup goes here"
            Case Columns("N").Column
                Debug.Print "N Lookup goes here"
            Case Columns("T").Column
                Debug.Print "T Lookup goes here"
        End Select
    End If

End Sub
 
Upvote 0
Dear Sir,
Compile Error

Duplicate Declaration in Current Scope
Did you put two procedures named "Worksheet_Change" in the same module? That is not allowed.
The code that we gave you is meant to replace what you currently have.
 
Upvote 0
Since the number of columns you are offsetting by is not the same for all three columns, I recommend you split them up into their own sections.

I have done the first one for you, you just need to enter the appropriate VLOOKUP formula for the other two.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Count > 1 Then Exit Sub

'   Column G check
    If Not (Intersect(Target, [G:G]) Is Nothing) Then
       Target.Offset(, 1) = Application.VLookup(Target, Sheet10.Range("A:B"), 2, False)
    End If
  
'   Column N check
    If Not (Intersect(Target, [N:N]) Is Nothing) Then
'       Enter your vlookup formula here
    End If
  
'   Column T check
    If Not (Intersect(Target, [T:T]) Is Nothing) Then
'       Enter your vlookup formula here
    End If

End Sub
Excellent & Thank of Lot it works perfect
 
Upvote 0
Oops if you are using mine remove the 1st 2 lines, I tested it as a normal Sub and had to declare and set target.

So the code should be:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Set rng = Range("G:G,N:N,T:T")
   
    If Not Intersect(Target, rng) Is Nothing And Target.Count = 1 Then
        Select Case Target.Column
            Case Columns("G").Column
                Debug.Print "G Lookup goes here"
            Case Columns("N").Column
                Debug.Print "N Lookup goes here"
            Case Columns("T").Column
                Debug.Print "T Lookup goes here"
        End Select
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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