VBA: Search for values in a column and copy row for all matching values

BMurphyNSTG

New Member
Joined
Mar 8, 2017
Messages
3
Hello,

I am trying to adapt a previous code to a new situation. I am new to VBA so I've been trying to teach myself on the job since then. The past two days I've been struggling with the following scenario.

I am searching column "AA" for values that appear in column "I". If the column "I" value = the "AA" value, then the cell to the right of the "AA" row is pasted in column "R" at the same row that the "I" value is in.
My main issue is getting the value to the right of "AA" ("AB") to paste into column "R".

Code:
Sub SearchForMatch()
   Const ISearchRow = 2
   Const AASearchRow = 2
   Dim CopyToRow As Integer
   Dim rng1 As Range
   Dim rng2 As Range
   Dim cell As Range
   Dim found As Range
   
   'Start copying data to row 2 in Sheet2 (row counter variable)
   CopyToRow = 2
   
   Set rng1 = Range(ActiveSheet.Cells(ISearchRow, 9), ActiveSheet.Cells(ISearchRow, 9).End(xlDown)) 'column I
   Set rng2 = Range(ActiveSheet.Cells(AASearchRow, 27), ActiveSheet.Cells(AASearchRow, 27).End(xlDown)) 'column AA
   
   For Each cell In rng2
   
   Set found = rng1.Find(what:=cell, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
   
   If Not found Is Nothing Then
   
   Cells(18, CopyToRow) = cell.EntireRow.Copy 'I think I need to shift the cells that I am pasting here
   'Paste to column R
   
   CopyToRow = CopyToRow + 1
   
   End If
   
   Next cell
End Sub

Any tips for how to introduce that paste of the cell to the right of the cell in column "AA" would be greatly appreciated! I'm not sure if a if/then statement would work more efficiently for what I am trying to achieve.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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