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".
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.
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.