VBA to lookup and enter values

JayB0730

Board Regular
Joined
Oct 22, 2014
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have two worksheets ("TransCode" and "Allocations"), and I would like to look up the values in the "TransCode" table from the first column ("Transaction Code"). Wherever there is a flag ("X"), I want to enter that in the appropriate column in the Allocations worksheet.

TransCode (This list could constantly grow - would like the loop to stop at the last row) :
Transaction CodeData 1Flag
ABCInformationX
DEFInformation
GHIInformationX
JKLInformationX
MNOInformation

Allocations Table
Transaction CodeData 1Data 2Data 3Flag
DEFInformationInformationInformation
JKLInformationInformationInformationThe code will place "X"
ABCInformationInformationInformationThe code will place "X"
MNOInformationInformationInformation
GHIInformationInformationInformationThe code will place "X"

I am trying to create a simple and fast code.

I appreciate any help you can provide.

Best,
J
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The realy super fast way of doing that is using the dictionary object in VBa try this code:
VBA Code:
Sub dicindexmatch()
   Dim Ary   As Variant
   Dim i As Long
   Dim Dic As Object
   Dim lastrow As Long
 
   Set Dic = CreateObject("Scripting.dictionary")
   With Worksheets("TransCode")
      lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
      Ary = .Range(.Cells(1, 1), .Cells(lastrow, 3))
   End With
   For i = 2 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 3)                ' Store the X in the dictionary
   Next i
   With Worksheets("Allocations")
      lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
      inarr = .Range(.Cells(1, 1), .Cells(lastrow, 1))
      outarr = .Range(.Cells(1, 5), .Cells(lastrow, 5))
  
      For i = 2 To lastrow
         If Dic.Exists(inarr(i, 1)) Then   ' check if element exists
         outarr(i, 1) = Dic(inarr(i, 1)) ' Copt Dictionary vvalue to output
         Else          ' if it doesn't set to not found
         outarr(i, 1) = "Not Found"
         End If
        
      Next i
      .Range(.Cells(1, 5), .Cells(lastrow, 5)) = outarr  ' write out the output array
    
   End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,454
Messages
6,124,931
Members
449,195
Latest member
Stevenciu

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