freerskys
New Member
- Joined
- Jul 24, 2014
- Messages
- 29
- Office Version
- 2010
- Platform
- Windows
Hello Friends,
This Macro will loop through rows to match column A on Sheet1 with column A on sheet2. On a non-match "No Match" will be put in Sheet1 column Q. On a match Sheet2 columns A,B,C,D and E will be copied to Sheet1 columns O,P,Q,R and S. What needs fixing is, while searching for an exact match for "193 AAA" it thinks 93 AAA is a match or 34 BBB = 3 BBB is a match, but it's not.
Any info is appreciated, thanks,
Sub MatchColumns()
Dim I, total, fRow As Integer
Dim found As Range
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
For I = 2 To total
answer1 = Worksheets(1).Range("A" & I).Value
Set found = Sheets(2).Columns("A:A").Find(what:=answer1) 'finds a match
If found Is Nothing Then
Worksheets(1).Range("Q" & I).Value = "NO MATCH"
Else
fRow = Sheets(2).Columns("A:A").Find(what:=answer1).Row
Worksheets(1).Range("O" & I).Value = Worksheets(2).Range("A" & fRow).Value
Worksheets(1).Range("P" & I).Value = Worksheets(2).Range("B" & fRow).Value
Worksheets(1).Range("Q" & I).Value = Worksheets(2).Range("C" & fRow).Value
Worksheets(1).Range("R" & I).Value = Worksheets(2).Range("D" & fRow).Value
Worksheets(1).Range("S" & I).Value = Worksheets(2).Range("E" & fRow).Value
End If
Next I
End Sub
This Macro will loop through rows to match column A on Sheet1 with column A on sheet2. On a non-match "No Match" will be put in Sheet1 column Q. On a match Sheet2 columns A,B,C,D and E will be copied to Sheet1 columns O,P,Q,R and S. What needs fixing is, while searching for an exact match for "193 AAA" it thinks 93 AAA is a match or 34 BBB = 3 BBB is a match, but it's not.
Any info is appreciated, thanks,
Sub MatchColumns()
Dim I, total, fRow As Integer
Dim found As Range
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
For I = 2 To total
answer1 = Worksheets(1).Range("A" & I).Value
Set found = Sheets(2).Columns("A:A").Find(what:=answer1) 'finds a match
If found Is Nothing Then
Worksheets(1).Range("Q" & I).Value = "NO MATCH"
Else
fRow = Sheets(2).Columns("A:A").Find(what:=answer1).Row
Worksheets(1).Range("O" & I).Value = Worksheets(2).Range("A" & fRow).Value
Worksheets(1).Range("P" & I).Value = Worksheets(2).Range("B" & fRow).Value
Worksheets(1).Range("Q" & I).Value = Worksheets(2).Range("C" & fRow).Value
Worksheets(1).Range("R" & I).Value = Worksheets(2).Range("D" & fRow).Value
Worksheets(1).Range("S" & I).Value = Worksheets(2).Range("E" & fRow).Value
End If
Next I
End Sub