Macro to copy specific cell based on two other cell contents being an exact match

exopolitixs

New Member
Joined
Mar 31, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi!

Hoping someone can assist (or point me in the right direction) of a Macro that will populate a column in sheet 1 with specific values from a column in sheet 2 based on the contents being an exact match.

My logic for this if content from cell L2 in Sheet 1 appears at all in column G then copy content from column A in the same row into Sheet 1 column A.

Context is I'm matching usernames from Sheet 1 and 2 to copy over specific user IDs.

Hoping someone can assist! I'm pulling my hair out trying to figure this out.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hello,

I am guessing that there are other names in Column L. Starting at L2

Also that Column G is in Sheet 2, copying from the A column on sheet 2, to column A on sheet 1

The following code does that.

VBA Code:
Sub GetID()
Dim i As Integer
Dim j As Integer
For j = 2 To 13 'starts at L2 ends at L13
For i = 1 To 12 'Looks through column G from row 1 to 12
    Application.ScreenUpdating = False
    If Cells(j, 12).Value = Sheets("Sheet2").Cells(i, 7).Value Then 'you can delete Sheets("Sheet2").  if column G is on sheet 1
    Sheets("Sheet2").Cells(i, 1).Copy 'Copies the sheet 2 A column of the match 
    Sheets("Sheet1").Cells(i, 1).Select 'Pastes it to Sheet 1 column A
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End If
Next i
Next j
    Application.ScreenUpdating = True
End Sub

The notes in green will help you change things. :)

Jamie
 
Upvote 0
Another option
VBA Code:
Sub exopolitixs()
   Dim Cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet2")
      For Each Cl In .Range("G2", .Range("G" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Cl.Offset(, -6).Value
      Next Cl
   End With
   With Sheets("Sheet1")
      For Each Cl In .Range("L2", .Range("L" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Cl.Offset(, -11).Value = Dic(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Solution
Another option
VBA Code:
Sub exopolitixs()
   Dim Cl As Range
   Dim Dic As Object
  
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet2")
      For Each Cl In .Range("G2", .Range("G" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Cl.Offset(, -6).Value
      Next Cl
   End With
   With Sheets("Sheet1")
      For Each Cl In .Range("L2", .Range("L" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Cl.Offset(, -11).Value = Dic(Cl.Value)
      Next Cl
   End With
End Sub
This worked perfectly, thank you so much!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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