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
Button Trigger

<tbody>
</tbody>

Sheet1
AB
name_categoryproduct_id
Television233431
Computer2334

<tbody>
</tbody>


Sheet2
AB
codename of category
TEL01Television
COM01Computer

<tbody>
</tbody>


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
In sheet 2 the code is in A and the cat. in B. But in sheet 1 the info is in D. Perhaps i swapped the vars.
 
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,215,731
Messages
6,126,539
Members
449,316
Latest member
sravya

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