Vlookup Column in another sheet

cssfonseca

New Member
Joined
Aug 14, 2018
Messages
19
Hello.
I have a Table where i have values in Column A. And i want to lookup dat value in sheet2, and replace it and the same column. All of this with the trigger of a button.

I have stumbled around many codes, but none of them seems to fit my purpose.

Explained
[TABLE="width: 500"]
<tbody>[TR]
[TD]Button Trigger[/TD]
[/TR]
</tbody>[/TABLE]

Sheet1
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]name_category[/TD]
[TD]product_id[/TD]
[/TR]
[TR]
[TD]Television[/TD]
[TD]233431[/TD]
[/TR]
[TR]
[TD]Computer[/TD]
[TD]2334[/TD]
[/TR]
</tbody>[/TABLE]


Sheet2
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]code[/TD]
[TD]name of category[/TD]
[/TR]
[TR]
[TD]TEL01[/TD]
[TD]Television[/TD]
[/TR]
[TR]
[TD]COM01[/TD]
[TD]Computer[/TD]
[/TR]
</tbody>[/TABLE]


So I want the value of the name of category to be replaced for the code, in sheet1, on the click of the button. My main problem is how to define the range, since i dont have a fixed size to the columnA.

Can somebody help me with this code?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Option Explicit


Sub foo()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Dim lr1 As Long, lr2 As Long
    lr1 = s1.Range("A" & Rows.Count).End(xlUp).Row
    lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row
    Dim i As Long, j As Long
    Application.ScreenUpdating = False
    For i = 2 To lr1
        For j = 2 To lr2
            If s2.Range("B" & j) = s1.Range("A" & i) Then
                s1.Range("A" & i) = s2.Range("A" & j)
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub
 
Upvote 0
Another option
Code:
Sub replacecat2()
   Dim cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("sheet1")
   Set Ws2 = Sheets("Pcode")
   
   With CreateObject("scripting.dictionary")
      For Each cl In Ws2.Range("B2", Ws2.Range("B" & Rows.Count).End(xlUp))
         .Item(cl.Value) = cl.Offset(, -1).Value
      Next cl
      For Each cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         cl.Value = .Item(cl.Value)
      Next cl
   End With
End Sub
 
Upvote 0
It worked perfectly! i just had to replace the columns for the one i wanted. Easiest lookup ever. Thank you for the fast reply :) :)
 
Upvote 0
Not sure who you're talking too, but glad we could help & thanks for the feedback
 
Upvote 0
Code:
Option Explicit


Sub foo()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Dim lr1 As Long, lr2 As Long
    lr1 = s1.Range("A" & Rows.Count).End(xlUp).Row
    lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row
    Dim i As Long, j As Long
    Application.ScreenUpdating = False
    For i = 2 To lr1
        For j = 2 To lr2
            If s2.Range("B" & j) = s1.Range("A" & i) Then
                s1.Range("A" & i) = s2.Range("A" & j)
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub

So its running perfect, except when i look for more than one hundred values. Sheet 3 has over 100.000 values. Should i give up for its to heavy for my pc?
 
Upvote 0
Another option
Code:
Sub replacecat2()
   Dim cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("sheet1")
   Set Ws2 = Sheets("Pcode")
   
   With CreateObject("scripting.dictionary")
      For Each cl In Ws2.Range("B2", Ws2.Range("B" & Rows.Count).End(xlUp))
         .Item(cl.Value) = cl.Offset(, -1).Value
      Next cl
      For Each cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         cl.Value = .Item(cl.Value)
      Next cl
   End With
End Sub

.Item(cl.Value) = cl.Offset(, -1).Value
i have an error on this line of code.
[h=1]Application-defined or object-defined error. Where did i went wrong?[/h]
 
Upvote 0
What columns are the Code & category in on sheet2?
 
Upvote 0
Ok, how about
Code:
Sub replacecat2()
   Dim cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("sheet1")
   Set Ws2 = Sheets("Sheet2")
   
   With CreateObject("scripting.dictionary")
      For Each cl In Ws2.Range("B2", Ws2.Range("B" & Rows.Count).End(xlUp))
         .Item(cl.Value) = cl.Offset(, -1).Value
      Next cl
      For Each cl In Ws1.Range("D2", Ws1.Range("D" & Rows.Count).End(xlUp))
         cl.Value = .Item(cl.Value)
      Next cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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