replace item for range with item for Table based on column

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hello
here is data in sheet1 as TABLE not range.
TABLE.xlsm
ABC
1ITEMBRANDQTY
21T01 185/65 R15188
321200R20 G580 JAP102
431200R20 R187 JAP133
541200R24 G580 JZ0
651300R22.5 R187 JAP147
761400R20 R180 JAP97
87175/65R14 B25A32 THI150
98175/70R13 EP150 THI4
109175/70R14 150 E2 THI30
1110185/65R14 EP150 INDO16
1211185/65R15 B250 JAP187
1312185/70R13 EP 150 INDO9
1413195/55R16 EP300 THI195
1514195/60R16 150EZ THI140
sheet1

and here is data in sheet2 as in range , not Table
TABLE.xlsm
ABC
1ITEMBRANDQTY
25BS 13R22.5 R187 JAP34
36BS 1400R20 R180 JAP55
43BS 1200R20 R187 JAP32
54BS 1200R24 G580 JAP12
67BS 175/65R14 B25A32 THI23
71BS 185/65R15 T001 JAP45
82BS 1200R20 G580 JAP67
98BS 175/70R13 EP150 THI67
109BS 175/70R14 ZE150 THI88
1112BS 185/70R13 EP150 INDO99
1213BS 195/55R16 EP300 THI80
1314FS 195/60R16 150EZ THI68
1410BS 185/65R14 EP150 INDO16
1511BS 185/65R15 B250 JAP187
sheet2

so I want replace items in sheet2 with item in sheet1 based on column A
the result in sheet1 should be
TABLE.xlsm
ABC
1ITEMBRANDQTY
21BS 185/65R15 T001 JAP188
32BS 1200R20 G580 JAP102
43BS 1200R20 R187 JAP133
54BS 1200R24 G580 JAP0
65BS 13R22.5 R187 JAP147
76BS 1400R20 R180 JAP97
87BS 175/65R14 B25A32 THI150
98BS 175/70R13 EP150 THI4
109BS 175/70R14 ZE150 THI30
1110BS 185/65R14 EP150 INDO16
1211BS 185/65R15 B250 JAP187
1312185/70R13 EP 150 INDO9
1413BS 195/55R16 EP300 THI195
1514FS 195/60R16 150EZ THI140
sheet1

and update in sheet1 automatically when add new or change the items when every time run the macro
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Sheet2 C2:
Excel Formula:
=VLOOKUP(A2,Table1,3,0)
EDIT: Sorry, I got it wrong.
 
Upvote 0
You must fix the table name:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Count = 1 And Not Intersect(Target, ListObjects("Table1").Range) Is Nothing Then
    Dim r As Boolean
    r = True
    Application.EnableEvents = False
    With ListObjects("Table1").Range
    For Each val2 In .Rows(Target.Row).Value2
      If val2 = "" Then
        r = False
      End If
    Next
    If r Then
      For i = .Cells(1, 1).Row + 1 To .Rows(.Rows.Count).Row
        For j = 2 To Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
          If .Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
            .Cells(i, 2).Value = Worksheets("Sheet2").Cells(j, 2).Value
          End If
        Next
      Next
    End If
    Application.EnableEvents = True
    End With
  End If
End Sub
 
Upvote 0
Or:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Count = 1 And Not Intersect(Target, ListObjects("Table1").Range) Is Nothing Then
    Application.EnableEvents = False
    With ListObjects("Table1").Range
    If Not IsEmpty(.Cells(Target.Row - (.Cells(1, 1).Row - 1), 1).Value2) Then
      For i = .Cells(1, 1).Row + 1 To .Rows(.Rows.Count).Row
        For j = 2 To Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
          If .Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
            .Cells(i, 2).Value = Worksheets("Sheet2").Cells(j, 2).Value
            Exit For
          End If
        Next
      Next
    End If
    Application.EnableEvents = True
    End With
  End If
End Sub
 
Upvote 0
Or get rid of check column A completely:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Count = 1 And Not Intersect(Target, ListObjects("Table1").Range) Is Nothing Then
    Application.EnableEvents = False
    With ListObjects("Table1").Range
      For i = 2 To .Rows(.Rows.Count).Row
        For j = 2 To Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
          If .Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
            .Cells(i, 2).Value = Worksheets("Sheet2").Cells(j, 2).Value
            Exit For
          End If
        Next
      Next
    End With
    Application.EnableEvents = True
  End If
End Sub
 
Last edited by a moderator:
Upvote 0
your last version is awesome !
can you make the macro in standard module instead of sheet module please?
 
Upvote 0
In standard module:
VBA Code:
Sub vLookup(ByVal Trgt As Range)
  If Trgt.Count = 1 And Not Intersect(Trgt, Worksheets("Sheet1").ListObjects("Table1").Range) Is Nothing Then
    Application.EnableEvents = False
    With Worksheets("Sheet1").ListObjects("Table1").Range
      For i = 2 To .Rows(.Rows.Count).Row
        For j = 2 To Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
          If .Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
            .Cells(i, 2).Value = Worksheets("Sheet2").Cells(j, 2).Value
            Exit For
          End If
        Next
      Next
    End With
    Application.EnableEvents = True
  End If
End Sub
To call it on worksheet cahge:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Call vLookup(Target)
End Sub
 
Last edited by a moderator:
Upvote 0
If you want only the change part then:
VBA Code:
    With Worksheets("Sheet1").ListObjects("Table1").Range
      For i = 2 To .Rows(.Rows.Count).Row
        For j = 2 To Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
          If .Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
            .Cells(i, 2).Value = Worksheets("Sheet2").Cells(j, 2).Value
            Exit For
          End If
        Next
      Next
    End With
Use this piece at whereever you want.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,338
Messages
6,124,358
Members
449,155
Latest member
ravioli44

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